title: Python shutil copyfile - missing last few lines
body: <p>I am routinely missing the last few kb of a file I am trying to copy using shutil copyfile.  </p>
  
  <p>I did some research and do see someone asking about something similar here:
  <a href="https://stackoverflow.com/questions/15303555/python-shutil-copy-function-missing-last-few-lines">python shutil copy function missing last few lines</a></p>
  
  <p>But I am using copyfile, which DOES seem to use a with statement...</p>
  
  <pre><code>with open(src, 'rb') as fsrc:
      with open(dst, 'wb') as fdst:
          copyfileobj(fsrc, fdst)
  </code></pre>
  
  <p>So I am perplexed that more users aren't having this issue, if indeed it is some sort of buffering issue - I would think it'd be more well known.  </p>
  
  <p>I am calling copyfile very simply, don't think I could possibly be doing something wrong, essentially doing it the standard way I think:</p>
  
  <pre><code>copyfile(target_file_name,dest_file_name) 
  </code></pre>
  
  <p>Yet I am missing the last 4kb or so of the file eachtime.</p>
  
  <p>I have also not touched the copyfile function which gets called in shutil which is...</p>
  
  <pre><code>def copyfileobj(fsrc, fdst, length=16*1024):
      """copy data from file-like object fsrc to file-like object fdst"""
      while 1:
          buf = fsrc.read(length)
          if not buf:
              break
          fdst.write(buf)
  </code></pre>
  
  <p>So I am at a loss, but I suppose I am about to learn something about flushing, buffering, or the with statement, or ... Help! thanks</p>
  
  <hr>
  
  <p>to Anand:
  Anand, I avoided mentioning that stuff bc it's my sense that it's not the problem, but since you asked... executive summary is that I am grabbing a file from an FTP, checking if the file is different from the last time I saved a copy, if so, downloading the file and saving a copy.  It's circuitous spaghetti code and was written when I was a truly pure utilitarian novice of a coder I guess.  It looks like:          </p>
  
  <pre><code>for filename in ftp.nlst(filematch):
      target_file_name = os.path.basename(filename)
      with open(target_file_name ,'wb') as 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 for 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
  </code></pre>