0

I'm having an issue with "FileNotFoundError: [Errno 2] No such file or directory: 'Pronunciation_Data.txt'

I keep resetting my working directory to the correct location but it does not seem to want to stay (using Spyder/Anaconda). I have also tried the following:

file = open(r'C:\path\to\Pronunciation_Data.txt\Users\stephaniecheetham\Desktop\Thesis')
import os
os.chdir(r'C:\Users\stephaniecheetham\Desktop\Thesis')
file = open('Pronunciation_Data.txt')

file = open("Pronunciation_Data.txt",'r')

file = open("<Users/stephaniecheetham/Desktop/Thesis>\Pronunciation_Data.txt",'r')

file = open("C:/stephaniecheetham/Desktop/Thesis/Pronunciation_Data.txt",'r')

No luck with any of them. Just the same error. I had a recent issue with a module as well and fixed it using the import os command.

Any suggestion?

kennysliding
  • 2,783
  • 1
  • 10
  • 31
  • The obvious answer is that you're in the wrong directory, or you're using the wrong filename. After doing `os.chdir()`, show us the output of this: `print(os.listdir())`. That will show us all the files that are in that directory. – John Gordon Dec 18 '20 at 03:36
  • 1
    Does this answer your question? [Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory](https://stackoverflow.com/questions/12201928/python-open-gives-filenotfounderror-ioerror-errno-2-no-such-file-or-directory) – Shounak Das Dec 18 '20 at 03:37

2 Answers2

1

Does your directory exist? Check if your file exists by doing this:

import os

os.chdir('C:\Users\stephaniecheetham\Desktop\Thesis')

try {
  file = open('Prononunciation_Data.txt')
} except FileNotFoundError {
  print("File not found")
}

Does your directory exist? Check if the directory exists by using os.listdir()

They are 2 mistakes in your code:

  • You have not imported the os module
  • They is a SyntaxError in line 2, where you're a missing 1 parenthese
Tech Helper
  • 51
  • 1
  • 4
0

To help with debugging, you can also do os.listdir() to see if you are actually in the correct directory.

Also, it's not clear what the full path of your file should be. C:/stephaniecheetham is different from C:/Users/stephaniecheetham

howdoishotweb
  • 149
  • 1
  • 1
  • 8