Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding files
#1
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.
Reply
#2
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
$
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding files matching pattern GrahamL 1 1,241 Jan-14-2022, 01:16 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020