I tried to check to see if any similar issues were reported here but couldn’t find. So my apologies if this was addressed before.
Actually I think my problem involves cwd. I am using macOS for a while now. Before that I was using win 7. I usually run python codes on Atom with “atom-python-run” pack and it’s just smoothly runs everything on both win 7 and macOS. However, on macOS, when it comes to python codes that require opening a simple file (txt in this case), it gives a traceback:
FileNotFoundError: [Errno 2] No such file or directory: ‘filename.txt’
I made some research and found out that it might be with the cwd but couldn’t figure how exactly it affects the run of the python line when both files are in the same directory. Some people suggested importing pathlib with a line:
filepath = pathlib.Path(__file__).parent / fname
when fname is: fname=input('Enter the file name: ')
I also found out that inputing the full path of the file to be opened is a solution too.
Both actually work well but when the assignments pile up, requiring me to work with many different file types in many different codes within a limited period of time, I think importing a lib when other learners aren’t or copying full path of the file each time, will mess things up.
On win7, the file I want to open via the python code on atom was as easy as to save the file in the same directory(folder) with the code file before f5/f6 it on Atom. On macOS, that does not work and I don’t know why.
I’ll share 2 screenshots;
-
I directly put the file name as a string within open() method and getting the Errno 2:
-
I input the file name on terminal window and still getting the Errno 2:
Atom is located on my desktop.
Python3 directory:
where python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/usr/local/bin/python3
/usr/bin/python3
Having left with no working solution I had also tried aliasing python to python3 on ~/.zshrc
where python
python: aliased to python3
/usr/bin/python
I appreciate for any help.
Regards.