You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Holle community, first of all thank you holzschu for making such a powerful app. Really great work.
I am new to programming and when attempting to open a simple *.txt file to loop over it for a book exercise I get a FileNotFoundError. I am sure I am missing something trivial.
[~/Documents/testdir]$ python open_file.py Traceback (most recent call last): File "/private/var/mobile/Containers/Data/Application/B7C98837-9423-45A4-A7E8-E17E40DF01AB/Documents/testdir/open_file.py", line 1, in <module> with open("~/Documents/testdir/test_text.txt") as f: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^FileNotFoundError: [Errno 2] No such file or directory: '~/Documents/testdir/test_text.txt'
When I type pwd I get:
[~/Documents/testdir]$ pwd~/Documents/testdir
And listing the contents of the directory shows the files I am working on:
Python sends the argument of "open" directly to the "open()" function, without any pre-treatment. "open()" is a low level function, it does not check anything in the file name. So the function tries to open a file named, litterally, ~/Documents/testdir/test_text.txt and finds that it doesn't exist. You need to first expand the ~ to its full value, using os.path.expanduser("~/Documents/testdir/test_text.txt").
Alternatively, you can replace "~" with os.environ["HOME"], as in: open(os.environ["HOME"] + "/Documents/testdir/test_text.txt").
You could also use a local file path, rather than an absolute file path: open("test_text.txt").
Holle community, first of all thank you holzschu for making such a powerful app. Really great work.
I am new to programming and when attempting to open a simple
*.txt
file to loop over it for a book exercise I get aFileNotFoundError
. I am sure I am missing something trivial.When I type
pwd
I get:And listing the contents of the directory shows the files I am working on:
My python code in open_file.py is:
Where is the recommendet path to store all my files? And again, thank you for your hard work.
Greetings from Bonn
Grigorij
The text was updated successfully, but these errors were encountered: