the man
the man

Reputation: 1391

No such file or directory but file exists?

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?

Upvotes: 10

Views: 45150

Answers (3)

Rintu Banerjee
Rintu Banerjee

Reputation: 124

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')

Upvotes: -1

AlpacaMax
AlpacaMax

Reputation: 479

  • 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.

Upvotes: 2

ErnstBusch
ErnstBusch

Reputation: 183

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')

Upvotes: 12

Related Questions