Home > Mobile >  I get FileNotFoundError in Python
I get FileNotFoundError in Python

Time:10-31

I'm using standard code that would open a text file in read mode. The compiler throws an error: "FileNotFoundError: [Errno 2] No such file or directory: 'Kinoteatr.txt'". I use Linux and PyCharm.

file_1 = open("Kinoteatr.txt", 'r')
content = file_1.readlines()
print(content)
file_1.close()

The python file is in the same directory as my txt file. I tried to explicitly specify the address to the file, renamed it and recreated it - it did not help.

CodePudding user response:

Check the current working directory of the script by doing:

import os
os.getcwd()

Then, compare this with your absolute path. Then you should be able to see if there are any inconsistencies.

CodePudding user response:

with open('file.txt') as f:
    cntread = f.read()
    cntlist = cntread.split('\n')
    print(cntlist)
  • Related