Python Forum
Opening files in same directory as script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opening files in same directory as script
#1
The textbook I am using didn't specify this problem so I want someone to help me.
I have a text file that is in the same directory as the script file. When I call file open, it tells me it cannot see it.
filehandle = open('box.txt')
The code returns an error that the file or directory doesn't exist.
What should I do to open it.
Reply
#2
Your code appears to be correct. Can you post the full traceback error message?
Reply
#3
Also, in which directory are you running the script from?
Reply
#4
(May-28-2020, 05:22 PM)GOTO10 Wrote: Your code appears to be correct. Can you post the full traceback error message?
The error message is:
FileNotFoundError: [Errno 2] No such file or directory: 'box.txt'

I have seen what the problem was just from a question a user asked.
I did not open the folder in vs code and the editor was reading from another folder. So, because someone asked about the folder, I decided to open the folder directly in vs code and it saw the file.
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to print directory files (& timestamps) chatguy 3 1,369 Dec-29-2022, 07:19 AM
Last Post: ndc85430
  traverses directory recursively and displays files Prani05 7 3,368 Apr-30-2020, 10:25 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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