英文:
Why does os.path refer to the project path instead of file path?
问题
这是文件夹的结构:
- python
- python学习
- study.py
- a.txt
study.py 中的代码是:
import os
if(os.path.exists("a.txt")):
print("是") # 我的真实代码不是这样的
else:
print("否")
通常,我会将“python学习”文件夹作为PyCharm项目打开,就像这样:
结果是“是”。
但今天,我不小心将“python”文件夹(而不是“python学习”)作为PyCharm项目打开并运行了代码。
结果是“否”。最后,我发现它是指的“python”(项目路径),而不是.py文件所在的文件夹。这有什么目的?
之后,我在cmd中运行这段代码,结果是“是”。
我认为有时候在与其他人开发项目时可能会带来一些问题(如果有人使用记事本而不是PyCharm来编辑代码,并在cmd中运行代码)。
英文:
Here is the structure of the folder:
- python
- python study
- study.py
- a.txt
The code in study.py is:
import os
if(os.path.exists("a.txt")):
print("YES") # my real code is not this
else:
print("NO")
Usually I open thepython study
folder as pycharm project,like this:
And the result is YES
.
But today I carelessly open the python
folder(not python study
) as pycharm project and run the code.
The result is NO
. Finally,I find it refer to the python
(project path) instead of the folder of the .py file.What's the purpose of this?
After this, I run this code in cmd,and the result is YES
.
I think it some time will bring some problems while develop project with other people.(If someone use notepad instead of pycharm to edit code and run code in cmd).
答案1
得分: 3
When you don't specify the absolute path of a file, it is assumed to be in the working directory.
When you open your project from the folder python
that is the working directory and os.path.exists('a.txt')
is actually looking for the file 'a.txt' in the folder python
.
In order to make your script independent of the working directory, you should use relative paths.
You can use __file__
to refer to your script. And you can get the directory of your script using os.path.dirname
.
So I think you should modify your code to:
import os
fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'a.txt')
if os.path.exists(fname):
print("YES")
else:
print("NO")
(Note: I've removed the HTML encoding in the code for better readability.)
英文:
When you don't specify the absolute path of a file, it is assumed to be in the working directory.
When you open your project from the folder python
that is the working directory and os.path.exists('a.txt')
is actually looking for the file 'a.txt' in the folder python
.
In order to make your script independant of the working directory. You should use relative paths.
You can use __file__
to refer to your script. And you can get the directory of your script using os.path.dirname
.
So I think you should modify your code to
import os
fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'a.txt')
if(os.path.exists(fname)):
print("YES")
else:
print("NO")
答案2
得分: 1
编辑:尝试使用这个而不是简单地调用路径存在,因为它应该规范化路径名路径的绝对版本:
os.path.abspath(os.curdir)
顺便说一下,根据[这个][1],如果没有被授予在请求的文件上执行os.stat()的权限,即使路径物理上存在,此函数可能返回False。所以要记住这一点。
[1]: https://docs.python.org/3/library/os.path.html#os.path.exists
英文:
Edit: try this instead of simply calling the path exist, cause it should normalize absolutized version of the pathname path:
os.path.abspath(os.curdir)
By the way, according to this, This function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists. So keep that in mind.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论