title: Moving Files after they are downloaded completly
body: <p>I’m trying to structure my downloads folder. I want to move all downloaded files to the appropriate folders. The program runs correctly when I move files to my Downloads folder or download small files from the Internet. However, when I download larger files, they are already moved before they are completely downloaded. As a result, they are without content after being moved. My idea is to wait until the download is complete. Does anyone have any idea how I can do that?</p>
  <pre><code>import time
  import os
  import shutil
  from watchdog.observers import Observer
  from watchdog.events import FileSystemEventHandler
  
  # path for tracking folder
  path = '/home/marius/Downloads'
  
  class OnMyWatch:
  
      path = path
    
      def __init__(self):
          self.observer = Observer()
    
      def run(self):
          event_handler = Handler()
          self.observer.schedule(event_handler, self.path, recursive = True)
          self.observer.start()
          try:
              while True:
                  time.sleep(1)
          except:
              self.observer.stop()
              print(&quot;Observer Stopped&quot;)
    
          self.observer.join()
    
    
  class Handler(FileSystemEventHandler):
    
      @staticmethod
      def on_any_event(event):
          
          check_dirs()       
  
          if event.is_directory:
              return None
    
          elif event.event_type == 'created':
              dir_list = os.listdir(path)
              move_files(dir_list)
              # Event is created, you can process it now
              print(&quot;Watchdog received created event - % s.&quot; % event.src_path)
  
          elif event.event_type == 'modified':
              dir_list = os.listdir(path)
              move_files(dir_list)
              # Event is modified, you can process it now
              print(&quot;Watchdog received modified event - % s.&quot; % event.src_path)
  
  # check if dirs exist
  def check_dirs():
          if not os.path.exists(f'{path}/Audio'):
              os.makedirs(f'{path}/Audio')
  
          if not os.path.exists(f'{path}/Bilder'):
              os.makedirs(f'{path}/Bilder')
  
          if not os.path.exists(f'{path}/Dokumente'):
              os.makedirs(f'{path}/Dokumente')
  
          if not os.path.exists(f'{path}/Videos'):
              os.makedirs(f'{path}/Videos')
  
          if not os.path.exists(f'{path}/Sonstiges'):
              os.makedirs(f'{path}/Sonstiges')
  
  # move files 
  def move_files(dir_list):
      text_files = ['.txt', '.pdf', '.docx', '.xlsx', '.pptx', '.odt']
      foto_files = ['.jpg', '.webp', '.png', '.tif', '.gif', '.jpeg', '.svg']
      video_files = ['.mp4']        
      audio_files = ['.ogg']
  
      for file in dir_list:
          if file.endswith(tuple(text_files)):
              file_path = f'{path}/{file}'
              if not os.path.exists(f'{path}/Dokumente/{file}'):
                  time.sleep(5)
                  shutil.move(file_path, f'{path}/Dokumente')
              else:
                  os.remove(file_path)
              
          if file.endswith(tuple(foto_files)):
              file_path = f'{path}/{file}'
              if not os.path.exists(f'{path}/Bilder/{file}'):
                  shutil.move(file_path, f'{path}/Bilder')
              else:
                  os.remove(file_path)
              
          if file.endswith(tuple(video_files)):
              file_path = f'{path}/{file}'
              if not os.path.exists(f'{path}/Videos/{file}'):
                  shutil.move(file_path, f'{path}/Videos')
              else:
                  os.remove(file_path)
  
          if file.endswith(tuple(audio_files)):
              file_path = f'{path}/{file}'
              if not os.path.exists(f'{path}/Audio/{file}'):
                  shutil.move(file_path, f'{path}/Audio')
              else:
                  os.remove(file_path)
  
  
  if __name__ == '__main__':
      watch = OnMyWatch()
      watch.run()
  </code></pre>