No such file or directory but file exists?

10,319

Solution 1

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

Solution 2

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

Share:
10,319

Related videos on Youtube

the man
Author by

the man

Updated on June 04, 2022

Comments

  • the man
    the man almost 2 years

    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?

    • Błotosmętek
      Błotosmętek about 4 years
      Location of your script is not important, what counts is your current directory (which doesn't have to be the same as the one containing the script). Preferably use full path to your file in open.
  • bruno desthuilliers
    bruno desthuilliers about 4 years
    "Make sure the file is really under the same directory with your code" => you may want to learn about the concept of "current working directory"...
  • bruno desthuilliers
    bruno desthuilliers about 4 years
    You're welcome - and thanks for having corrected your answer (downvote retracted).

Related