I'm new to coding and doing a coursera course, and we just got into opening and reading files. I have this file 'words.txt' and I'm trying to open and read it but keep getting:
FileNotFoundError: [Errno 2] No such file or directory: 'words.txt'
.
Even though the words.txt is in my pythonProject file and the working directory is:
Current working directory: /Users/me/PycharmProjects/pythonProject
I had to resort to the absolute path, but I don't want to do that everytime I open a file.
Do you have to use the absolute path every time even though the working directory is matched?
If you right click on your program and select "run" it will make a run configuration for your program. Then if you go up to the run config at the top and edit it, you'll get a screen as shown below, which shows the "working directory"
By default, it is the location of the program. If this is the case, the reference to words.txt
should be just the filename, words.txt
(current directory) and it should work fine.
If you ran it from pythonProject
(top level of your project), you'd need to change the file reference in your program to Module_2/words.txt
is is usually "standard" to run things from top level of project, but it depends on a lot of things you'll learn later...
You should also get in the habit of not using spaces in directory or file names because you will need to "escape" them to make them reference-able in text. (trust me). Just rename them.