title: Python script to move files based on file extension
body: <p>I'm trying to write a script to move files from my downloads folder to its appropriate folder based on file extension. A lot of the files in the downloads folder are contained in sub directories so I'm using os.walk. Here's what I have so far but I'm getting some error messages. I've only written to code for one filetype so far just to see if it would work I'll write the code for most common filetypes when I have this working. I'm on linux btw.</p>
  <pre><code>import os
  import shutil
  
  dest_dir = &quot;/home/kevin/Videos&quot;
  dest_dir2 = &quot;/home/kevin/Music&quot;
  dest_dir3 = &quot;/home/kevin/Documents&quot;
  dest_dir4 = &quot;/home/kevin/Pictures&quot;
  
  for root, dirs, files in os.walk(&quot;/home/kevin/Downloads&quot;):
      for file in files:
          if(file.endswith(&quot;.mp4&quot;)):
              print(os.path.join(root,file))
              shutil.move(file, dest_dir)
  </code></pre>
  <p>and here's the erorrs.</p>
  <pre><code>/home/kevin/Downloads/Mad Max Beyond Thunderdome 1985 1080p BDRip H264 AAC - KiNGDOM/Mad Max Beyond Thunderdome.mp4
  Traceback (most recent call last):
    File &quot;/usr/lib/python3.8/shutil.py&quot;, line 788, in move
      os.rename(src, real_dst)
  FileNotFoundError: [Errno 2] No such file or directory: 'Mad Max Beyond Thunderdome.mp4' -&gt; '/home/kevin/Videos/Mad Max Beyond Thunderdome.mp4'
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;/home/kevin/Documents/move.py&quot;, line 13, in &lt;module&gt;
      shutil.move(file, dest_dir)
    File &quot;/usr/lib/python3.8/shutil.py&quot;, line 802, in move
      copy_function(src, real_dst)
    File &quot;/usr/lib/python3.8/shutil.py&quot;, line 432, in copy2
      copyfile(src, dst, follow_symlinks=follow_symlinks)
    File &quot;/usr/lib/python3.8/shutil.py&quot;, line 261, in copyfile
      with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
  FileNotFoundError: [Errno 2] No such file or directory: 'Mad Max Beyond Thunderdome.mp4'
  [Finished in 0.0s with exit code 1]
  [shell_cmd: python -u &quot;/home/kevin/Documents/move.py&quot;]
  [dir: /home/kevin/Documents]
  [path: /home/kevin/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin]
  </code></pre>
  <p>any idea what I'm doing wrong? If I can get this working I was also wondering if there's a way to move the folders containing the files to the destination directory instead of just the files themselves? Thanks!</p>
  <p><strong>Edit:</strong> this part seems interesting as it's right in that directory doesn't exist but why is it looking for /home/kevin/Videos/Mad Max Beyond Thunderdome.mp4 when the actual directory is /home/kevin/Videos/Mad Max Beyond Thunderdome 1985 1080p BDRIP H264 AAC - KiNGDOM  ??</p>
  <pre><code>FileNotFoundError: [Errno 2] No such file or directory: 'Mad Max Beyond Thunderdome.mp4' -&gt; '/home/kevin/Videos/Mad Max Beyond Thunderdome.mp4'
  </code></pre>
  <p><strong>Edit 2:</strong> Here's a part of the code I wrote earlier for moving files from downloads to documents (pdf, doc, etc) and this code works but not for files contained within subfolders. is there any way I can modify this code to include subfolders? i.e use os.walk</p>
  <pre><code>#moves files from downloads to documents
  
  for file in glob.glob(r'/home/kevin/Downloads/*.txt'):
      print(file)
      shutil.move(file, dest_dir)
  
  for file in glob.glob(r'/home/kevin/Downloads/*.doc'):
      print(file)
      shutil.move(file, dest_dir)
  
  for file in glob.glob(r'/home/kevin/Downloads/*.docx'):
      print(file)
      shutil.move(file, dest_dir)
  
  for file in glob.glob(r'/home/kevin/Downloads/*.epub'):
      print(file)
      shutil.move(file, dest_dir)
  
  for file in glob.glob(r'/home/kevin/Downloads/*.pdf'):
      print(file)
      shutil.move(file, dest_dir)
  
  for file in glob.glob(r'/home/kevin/Downloads/*.djvu'):
      print(file)
      shutil.move(file, dest_dir)
  </code></pre>
  <p><strong>Edit 3:</strong> Think I found a messy solution. apparently glob has a recursive function so I changed the code to something like this</p>
  <pre><code>for file in glob.glob('/home/kevin/Downloads/**/*.txt', recursive=True):
  </code></pre>
  <p>it's throwing up some error messages but it is actually working now.</p>