Python Forum

Full Version: Finding files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using an iMac with OS 10.12.6. I downloaded 3.7.1, and, using IDLE in the shell, I tried to read an existing text file with
f = open("entity_list.txt")
which resulted in a message that the file could not be found. The file is in the same folder that the Python was installed in.
You should give more details about the error.
I tested your code, and I founded it works. The error in the first run is because there is no file (this is normal), and that was corrected by creating a file (touch command).
The test was made on linux, but I'm pretty sure you will get the same results on another OS.
You could check there is a real file, at the right place, with the correct access right, and give us a feedback.

$ cat openFile.py
f = open("entity_list.txt")

$ python openFile.py
Traceback (most recent call last):
File "openFile.py", line 1, in <module>
f = open("entity_list.txt")
IOError: [Errno 2] No such file or directory: 'entity_list.txt'

$ touch entity_list.txt
$ python openFile.py
$ python3 openFile.py
$
You can get more information from python
from pathlib import Path
wd = Path.cwd()
print("The current directory is {}".format(wd))
for p in wd.iterdir():
    print(p.name)