Reading a File in Python

FileNotFoundError Traceback (most recent call last)
----> 1 with open(‘Ricardo Sobalvarro Internship reflection Week’) as hmwk:
2 file_hmwk =
3 print(file_hmwk)

FileNotFoundError: [Errno 2] No such file or directory: ‘Ricardo Sobalvarro Internship reflection Week’

This is my code:
with open(‘Ricardo Sobalvarro Internship reflection Week’) as hmwk:
file_hmwk =

PLEASE HELP! Why does my computer not the read the document when it is saved on my computer?

Hey there, welcome to the forums.

Python will default to trying to find the file in whatever directory the script is running in, so if your Python file is not in the directory with the file you’re trying to read then you’ll get that error.

You may have better luck if you pass an absolute path to Python, for example:

with open('~/user/random/my_file.docx') as file:

Since you’re specifying the path to the file from a well understood root (*nix home in this case) you shouldn’t get the error. :slight_smile:


Hello thanks for the quick and informative response! :slight_smile:

May you advise why I get this issue?

File “”, line 1
with open(‘C:\Users\msoba\Documents\Codecademy\SQL\Projects\Potential Code.txt’) as hmwk:
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

It’s because the \ backslash is an escape character in a Python string (e.g. \n for a new line character). You’d need to modify this to take care of the backslash. Either use a raw string (r'C:\example\noescapes') or use \\ two backslashes which produces a single backslash character in the string.

Try printing them out if you wanted to see the difference.



It is perhaps better to use relative addressing on the local machine. CD to navigate to the directory of the script, and determine the path from there to the file directory (if it’s different).


Thank you I was able to make the code run by switching from / to \ :+1: