title: File exist on os.listdir but is not accessible because it is still being copied
body: <p>I have a python script where <code>while(1)</code> loop looks for an image files in a specific folder with <code>os.listdir</code>.
  If any of supported format is detected it is converted to <code>PNG</code> with <code>PIL</code> library.
  From time to time some other application copies some files (5MB) to that directory which takes some time.
  The problem is that <code>os.listdir</code> detects a presence of each file at the very beginning of the copying process, but unfortunately those are not usable until copying is fully finished.</p>
  <p>Before copying is finished opening a file doesn't throw any exception and checking for an access to the file with <code>os.access(path, os.R_OK)</code> is also ok.</p>
  <p>Do You have any idea how to make sure that all files reported by os.listdir are usable, so in my case fully copied?</p>
  <pre><code>import time
  import os
  import shutil
  import Image
  
  #list of image formats supported for conversion
  supported_formats = ['bmp', 'tga']
  output_format = 'png'
  output_prefix = 'prefix_'
  
  
  def find_and_convert_images(search_path, destination_path, output_img_prefix, new_img_format):
      for img_file in os.listdir(search_path):
          if img_file[-3:] in supported_formats:
              print(&quot;Converting image: &quot; + str(img_file))
              convert_image(os.path.join(search_path, img_file), new_img_format)
              converted_img_name = img_file[:-3] + new_img_format
              new_img_name = output_img_prefix + img_file[:-3] + new_img_format
              if not os.path.isdir(destination_path):
                  os.makedirs(destination_path)
              try:
                  shutil.move(os.path.join(search_path, converted_img_name), os.path.join(destination_path, new_img_name))
              except Exception, error:
                  print(&quot;Failed to move image: &quot; + converted_img_name + &quot; with error: &quot; + str(error))
  
  
  def convert_image(img_file, new_img_format):
      try:
          img = Image.open(img_file)
          img.save(img_file[:-3] + new_img_format)
          del img
      except Exception, error:
          print(&quot;Failed convert image: &quot; + img_file + &quot; with error: &quot; + str(error))
      try:
          os.remove(img_file)
      except Exception, error:
          print(&quot;Failed to remove image: &quot; + img_file + &quot; with error: &quot; + str(error))
  
  
  def main():
      images_directory = os.path.join(os.getcwd(), 'TGA')
      converted_directory = os.path.join(images_directory, 'output')
      while 1:
          find_and_convert_images(images_directory, converted_directory, output_prefix, output_format)
  </code></pre>
  <p>The output is as follows:</p>
  <blockquote>
  <p>Converting image: image1.tga</p>
  <p>Failed convert image: /TEST/TGA/image1.tga with error: cannot identify image file</p>
  <p>Failed to move image: image1.png with error: [Errno 2] No such file or directory: '/TEST/TGA/image1.png'</p>
  </blockquote>
  <p>If I have copied tga files to TGA folder before running python script all is ok and pictures are converted and moved to other direcroty without any errors.</p>