Python shutil copyfile - missing last few lines -


i routinely missing last few kb of file trying copy using shutil copyfile.

i did research , see asking similar here: python shutil copy function missing last few lines

but using copyfile, seem use statement...

with open(src, 'rb') fsrc:     open(dst, 'wb') fdst:         copyfileobj(fsrc, fdst) 

so perplexed more users aren't having issue, if indeed sort of buffering issue - think it'd more known.

i calling copyfile simply, don't think possibly doing wrong, doing standard way think:

copyfile(target_file_name,dest_file_name)  

yet missing last 4kb or of file eachtime.

i have not touched copyfile function gets called in shutil is...

def copyfileobj(fsrc, fdst, length=16*1024):     """copy data file-like object fsrc file-like object fdst"""     while 1:         buf = fsrc.read(length)         if not buf:             break         fdst.write(buf) 

so @ loss, suppose learn flushing, buffering, or statement, or ... help! thanks


to anand: anand, avoided mentioning stuff bc it's sense it's not problem, since asked... executive summary grabbing file ftp, checking if file different last time saved copy, if so, downloading file , saving copy. it's circuitous spaghetti code , written when pure utilitarian novice of coder guess. looks like:

for filename in ftp.nlst(filematch):     target_file_name = os.path.basename(filename)     open(target_file_name ,'wb') fhandle:     try:         ftp.retrbinary('retr %s' % filename, fhandle.write)         the_files.append(target_file_name)         mtime = modification_date(target_file_name)         mtime_str_for_file = str(mtime)[0:10] + str(mtime)[11:13] + str(mtime)[14:16]    + str(mtime)[17:19] + str(mtime)[20:28]#2014-12-11 15:08:00.338415.         sorted_xml_files = [file file in glob.glob(os.path.join('\\\\storage\\shared\\', '*.xml'))]         sorted_xml_files.sort(key=os.path.getmtime)         last_file = sorted_xml_files[-1]         file_is_the_same = filecmp.cmp(target_file_name, last_file)         if not file_is_the_same:             print 'file changed!'             copyfile(target_file_name, '\\\\storage\\shared\\'+'datebreaks'+mtime_str_for_file+'.xml')          else:             print 'file '+ last_file +' hasn\'t changed, doin nothin'             continue 

the issue here , when executing line -

ftp.retrbinary('retr %s' % filename, fhandle.write) 

this using fhandle.write() function write data ftp server file (with name - target_file_name) , time calling -shutil.copyfile - buffer fhandle has not flushed, missing out on data when copying file.

to make sure not occur, can either move copyfile logic out of with block fhandle .

or can call fhandle.flush() flush buffer , before copying file .

i believe better close file (move logic out of with block). example -

for filename in ftp.nlst(filematch):     target_file_name = os.path.basename(filename)     open(target_file_name ,'wb') fhandle:         ftp.retrbinary('retr %s' % filename, fhandle.write)     the_files.append(target_file_name)     mtime = modification_date(target_file_name)     mtime_str_for_file = str(mtime)[0:10] + str(mtime)[11:13] + str(mtime)[14:16]    + str(mtime)[17:19] + str(mtime)[20:28]#2014-12-11 15:08:00.338415.     sorted_xml_files = [file file in glob.glob(os.path.join('\\\\storage\\shared\\', '*.xml'))]     sorted_xml_files.sort(key=os.path.getmtime)     last_file = sorted_xml_files[-1]     file_is_the_same = filecmp.cmp(target_file_name, last_file)     if not file_is_the_same:         print 'file changed!'         copyfile(target_file_name, '\\\\storage\\shared\\'+'datebreaks'+mtime_str_for_file+'.xml')      else:         print 'file '+ last_file +' hasn\'t changed, doin nothin'         continue 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -