No such file or directory but file exists?

Viewed 14958

I'm writing a python script where I need to open a ".txt" folder and analyse the text in there.

I have saved this ".txt" document in the same folder as my Python script.

But, when I go to open the file; file = open("words.txt",'r')

I get the error: No such file or directory: 'words.txt'.

I don't understand why this is happening?

3 Answers

Maby it's because your current working directory is different from the directory your files are stored. Try giving the full path to the file

file = open("<full_path>\words.txt",'r')

  • Check if there's a typo in the code or in the filename of the file
  • Make sure the file is really under the current working directory. Sometimes similar filenames or info shown in your IDE cause confusions
  • Make sure your are editing the correct script. Sometimes people copy and paste a script into different places and immediately forgot which one they are actually editing

Hope it helps.

It's because the directory of the .py file you are working with is not the same as the .txt file's path.

So, you need to mention the path like this:

file = open("C:/User/Desktop/Folder/words.txt", 'r')
Related