python - shutil.copyfile does not copy all lines -


77 lines missing temporary output file clonetemp.txt, when using shutil.copyfile(). why this?

this file contains 2 functions. first function creates temporary file contain data generated file called newfilelocation created my main.py

import os import sys import shutil       def clonetemp(newfilelocation):         clonefile = open('clonetemp.txt', 'w+')         shutil.copyfile(newfilelocation, 'clonetemp.txt') 

this second function send new lines beginning of clonetemp.txt, copy data newfilelocation clonetemp.txt , finally, write @ end of clonetemp.txt:

def gridenable(newfilelocation):      if(x < 200):         print x         clonetemp.write(x)     else:         clonetemp.write(y)      shutil.copyfile('clonetemp.txt', newfilelocation) 

yet, lines missing when it's copied over. why newfilelocation not being copied in it's entirety?

edit

newfilelocation file containing output main.py, all. since can't prepend beginning of newfilelocation, creating temporary file of newfilelocation called clonetemp.txt copy on data only after i've added lines empty clonetemp.txt require. after lines added, shutil.copyfile() should copy newfilelocation in it's entirety clonetemp.txt. yet, not.

i'm still confused want here strawman answer. code take file in newfilelocation , add data front , of file.

with open('clonetemp.txt', 'w') tmp:     tmp.write(x)     open(newfilelocation) newfile:         shutil.copyfileobj(newfile, tmp)     tmp.write(y) os.rename('clonetemp.txt', newfilelocation) 

so, lets test

import shutil import os  # file test newfilelocation = 'testfile.txt' open(newfilelocation, 'w').write('this\nis\na\ntest\n') print('before:') print(open(newfilelocation).read())  open('clonetemp.txt', 'w') tmp:     tmp.write('header\n')     open(newfilelocation) newfile:         shutil.copyfileobj(newfile, tmp)     tmp.write('footer\n') os.rename('clonetemp.txt', newfilelocation) print('after:') print(open(newfilelocation).read()) 

produces

before: test  after: header test footer 

Comments