程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python relative path FileNotFoundError: [Errno 2] No such file or directory: nihao.txt

編輯:Python
Problem and background

macOS Monterey12.3
python3.10

At first, the atom-python-run in atom cannot run. After modifying the F5 command to python3 {file}, the program can be run, but an error is reported when opening a program with a relative path.

After testing, cd to the folder can run normally, but when opening through the path, the error FileNotFoundError: [Errno 2] No such file or directory: 'nihao.txt'


py program and txt file in one folder

x=open('nihao.txt')
print(x)

Running result and error content


Error when opening via path


cd to the folder is normal

My desired outcome

Is there any way to make the program run by changing the mac settings without changing the program?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved