-5

For some reason I am unable to open my .txt file within python.

I have the .py and .txt file within a folder. Both files are stored Workspace -> Folder(Crash Course) -> Folder(Lessons) -> Folder(Ch 10)-> both files within this Ch 10 Folder.

I am getting

FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt'

With the code:

with open('pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)
Jvnior
  • 1
  • 2
  • 3
    There is no file with the name `pi_digits.txt` in your present working directory – bigbounty Apr 15 '19 at 18:42
  • 2
    Depending on how you are running Python, even if both the `.py` and `.txt` files are in the same directory, Python may not be operating in the right working directory as @bigbounty points out. Try using a full path for the file. – Brendan A. Apr 15 '19 at 18:43
  • Check, double check, and triple check that the file actually exists in the current working directory. I can't tell you how many times this error has occurred in both my code and other's because the file doesn't exist. – Pika Supports Ukraine Apr 15 '19 at 18:44
  • I think maybe your file is not in your folder. Also if you want to read the file use: `open('your_fle', 'r')`. – Esdras Xavier Apr 15 '19 at 18:44
  • What is the name of your file? – WhipStreak23 Apr 15 '19 at 19:01
  • bigbounty states it clearly, @BrendanA. If python was not **operating** in the right directory, the Python file would not run. – WhipStreak23 Apr 15 '19 at 19:03

6 Answers6

2

This is less for the person that asked the question but more for people like myself that come here from Python Crash Course with the same question and don't get the answer they were looking for:

If, like me, you were running the code from your text editor (in my case VS Code), it's possible that the terminal window within the editor wasn't in the proper directory. I didn't realize myself, because I was thinking that because I opened the .py file from the correct working directory in the terminal that everything should work as planned. It wasn't until I realized that the terminal in the editor is a separate instance (thus making the present working directory home instead of my folder for PCC work) that I was able to get the program to run as intended.

In short, navigate to the proper directory in your editor's terminal instance and the program should run as intended.

Hope this helps!

image with terminal open on desktop and in text editor to show working directory difference

Jin
  • 21
  • 4
2

I used full path of the file along with r, which is for raw string. Worked for me. example:

filename = **r**'C:\Python\CrashCourse\pi_digits.txt'

with open(filename) as file_object:
    content = file_object.read()
    print(content)
Ironkey
  • 2,568
  • 1
  • 8
  • 30
0

The path to the file is relative to where you run the python file from, not from where the python file is located.

Either run your code from the same directory as the files, or make the file path absolute, based on the python file's location.

import os

with open(os.path.join(os.path.dirname(__file__), 'pi_digits.txt')) as file_object:
    contents = file_object.read()
    print(contents)

Hope that helps

OsmosisJonesLoL
  • 244
  • 1
  • 4
0

You can try getting the full path to the file

import os

dir_path = os.path.dirname(os.path.realpath(__file__))
pi_digits = os.path.join(dir_path, 'pi_digits.txt')

with open(pi_digits, r) as file_object:
    print(file_object.read())
Mikey Lockwood
  • 1,258
  • 1
  • 9
  • 22
0

Try this:

with open('c:\\Workspace\\Crash Course\\Lessons\\Ch 10\\pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
  • Hmm, I just tried, think may be a problem with my file being stored within iCloud. I copied the path of the file and still got the same error. `with open('c:/Users/junior/Library/Mobile Documents/com~apple~CloudDocs/Desktop/python_work/Python Crash Course/Lessons/Ch 10/pi_digits.txt') as file_object: contents = file_object.read() print(contents)` I will test it out right now by doing files on desktop. Also thank you @esdras Xaviers. See now you can open & read it all in 1 line. – Jvnior Apr 15 '19 at 19:15
  • It worked now with file path, just had to remove 'c:' Thank you! – Jvnior Apr 15 '19 at 19:47
-1

You might have to enable "Execute in file dir" vscode setting