title: File not found error in os.rename
body: <p>I am trying to write a program to categorize into folders a large amount of files according to their respective groups indicated in the file name. I wrote the followin code, but when I run it it gives me a file not found error, even though the file is in the given path. I'd appreciate any help in figuring out what is wrong.</p>
  
  <pre><code>import os
  
  old_dir = '/Users/User/Desktop/MyFolder'
  
  for f in os.listdir(old_dir):
      file_name, file_ext = os.path.splitext(f)
      file_name.split('-')
  
      split_file_name = file_name.split('-')
  
      new_dir = os.path.join(old_dir,
                             '-'.join(split_file_name[:3]),
                             split_file_name[5],
                             f)
  
      os.rename(os.path.join(old_dir, f), new_dir)
  </code></pre>
  
  <p>Here's the error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/User/Documents/Sort Files into Folders/Sort Files into Folders.py", line 19, in &lt;module&gt;
      os.rename(os.path.join(old_dir, f), new_dir)
  FileNotFoundError: [Errno 2] No such file or directory: '/Users/User/Desktop/MyFolder/AHA35-3_30x1_12-31-7d-g1a1-ArmPro.jpg' -&gt; '/Users/User/Desktop/MyFolder/AHA35-3_30x1_12-31/ArmPro/AHA35-3_30x1_12-31-7d-g1a1-ArmPro.jpg
  </code></pre>