Eissen Eissen - 10 months ago 69
Python Question

FileNotFoundError WinError 3

I'm trying to learn how to edit files, but I'm a bit of a python novice, and not all that bright, so when I get a FileNotFoundError I can't figure out how to fix it despite several searches on the interwebz.

import os

old = 'Users\My Name\Pictures\2013\182904_10201130467645938_341581100_n'
new = 'Users\My Name\Pictures\2013\Death_Valley_1'

os.rename(old, new)


'Users\My Name\Pictures\2013\182904_10201130467645938_341581100_n' is a relative path.

Unless you are running your code from the directory that contains the Users dir (which if you are using Windows would most probably be the root C: dir), Python isn't going to find that file.

You also have to make sure to include the file extension if it has any.

There are few ways to solve this, the easiest one will be to use the absolute paths in your code, ie 'C:\Users\My Name\Pictures\2013\182904_10201130467645938_341581100_n.jpg'.

You will also want to use r before the paths, so you want need to escape every \ character.

import os

old = r'C:\Users\My Name\Pictures\2013\182904_10201130467645938_341581100_n.jpg'
new = r'C:\Users\My Name\Pictures\2013\Death_Valley_1.jpg'

os.rename(old, new)

This of course assumes your drive letter is C.