为什么python有os.path.curdir

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14512087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:40:54  来源:igfitidea点击:

Why does python have os.path.curdir

python

提问by boatcoder

os.path.curdirreturns '.' which is totally truthful and totally worthless. To get anything useful from it, you have to wrap it with os.path.abspath(os.path.curdir)

os.path.curdir返回'.' 这是完全真实的,完全没有价值。要从中获得任何有用的东西,你必须用os.path.abspath(os.path.curdir)

Why include a useless variable in the os.path module? Why not have os.path.curdir be a function that does the os.path.abspath for you?

为什么在 os.path 模块中包含一个无用的变量?为什么不让 os.path.curdir 成为一个为你做 os.path.abspath 的函数?

Is there some historic reason for os.path.curdirto exist?

是否存在某种历史原因os.path.curdir

Maybe useless is a bit harsh, but not very useful seems weak to describe this. enter image description here

也许没用有点苛刻,但不是很有用似乎很难描述这一点。 在此处输入图片说明

采纳答案by Martijn Pieters

It is a constant, just like os.path.sep.

它是一个常数,就像os.path.sep

Platforms other than POSIX and Windows could use a different value to denote the 'current directory'. On Risc OS it's @for example, on the old Macintosh OS it's :.

POSIX 和 Windows 以外的平台可以使用不同的值来表示“当前目录”。@例如,在 Risc OS上,在旧的 Macintosh OS 上它是:.

The value is used throughout the standard library to remain platform agnostic.

该值用于整个标准库以保持平台不可知。

Use os.getcwd()instead; os.path.abspath()uses that function under the hood to turn os.path.curdirinto the current working directory anyway. Here is the POSIX implementation of abspath():

使用os.getcwd()代替; 无论如何os.path.abspath(),都会在幕后使用该功能os.path.curdir转换为当前工作目录。这是 POSIX 实现abspath()

def abspath(path):
    """Return an absolute path."""
    if not isabs(path):
        if isinstance(path, _unicode):
            cwd = os.getcwdu()
        else:
            cwd = os.getcwd()
        path = join(cwd, path)
    return normpath(path)

回答by cdhowie

It's just a constant, platform-dependent value. From the docs(which are worth reading):

它只是一个恒定的、依赖于平台的值。从文档(值得一读):

The constant string used by the operating system to refer to the current directory. This is '.'for Windows and POSIX. Also available via os.path.

操作系统用来引用当前目录的常量字符串。这适用'.'于 Windows 和 POSIX。也可通过os.path.

You might consider using os.getcwd()instead.

您可以考虑使用os.getcwd()

回答by Dietrich Epp

The value of os.path.curdiris "."on Linux, Windows, and OS X. It is, however, ":"on old Mac OS 9 systems. Python has been around long enough that this used to be important.

的值os.path.curdir适用"."于 Linux、Windows 和 OS X。但是,它适用":"于旧的 Mac OS 9 系统。Python 已经存在了很长时间,以至于这曾经很重要。