0

How do I open a file in a Python program if the error I'm getting says the file doesn't exist? It exists in my fileexplorer.

Error message:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'Test.txt'

I tried to open a different file, which also didn't work. Do I need to transfer the file to another directory, or try a different line of code?

quamrana
  • 37,849
  • 12
  • 53
  • 71
Kay
  • 1
  • 1
  • 2
    Try using an absolute pathname. You're probably using a relative path and your current working directory isn't where you thought it was – DarkKnight Jul 04 '23 at 16:57
  • If you open a file like this `open("Test.txt")` then place it in directory where you run the script. Otherwise check your running directory and file location. – Andrey Ivanov Jul 04 '23 at 16:58
  • Do the answers to this [question](https://stackoverflow.com/questions/61586037/how-to-open-file-located-in-same-folder-as-python-script) help at all? – quamrana Jul 04 '23 at 16:58
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jul 05 '23 at 16:16

1 Answers1

0

Try to put the address of the file as full pathname rather than relative!

C:\home\sally\statusReport

Above is the example of absolute path