为什么 os.path 引用项目路径而不是文件路径?

huangapple go评论78阅读模式
英文:

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项目打开,就像这样:

为什么 os.path 引用项目路径而不是文件路径?

结果是“是”。

但今天,我不小心将“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:

为什么 os.path 引用项目路径而不是文件路径?

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.

huangapple
  • 本文由 发表于 2020年1月3日 20:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定