为什么没有路径。join()在这种情况下工作?

时间:2023-01-17 03:47:15

The below code will not join, when debugged the command does not store the whole path but just the last entry.

下面的代码将不会加入,当调试该命令时,它不会存储整个路径,而是只存储最后一个条目。

os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/')

When I test this it only stores the /new_sandbox/ part of the code.

当我测试它时,它只存储/new_sandbox/部分代码。

11 个解决方案

#1


296  

The latter strings shouldn't start with a slash. If they start with a slash, then they're considered an "absolute path" and everything before them is discarded.

后面的字符串不应该以斜线开头。如果它们以斜线开头,那么它们就被认为是“绝对路径”,在它们之前的所有东西都被丢弃。

Quoting the Python docs for os.path.join:

为os.path.join引用Python文档:

If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

如果一个组件是一个绝对路径,那么所有先前的组件都会被丢弃,并从绝对路径组件继续连接。

Note on Windows, the behaviour in relation to drive letters, which seems to have changed compared to earlier Python versions:

关于Windows的注意事项,与驱动字母相关的行为,与早期的Python版本相比似乎已经发生了变化:

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

在Windows上,当遇到绝对路径组件(例如,r'\foo')时,驱动器字母不会被重置。如果一个组件包含一个驱动器号,那么所有先前的组件都将被丢弃,驱动器号将被重置。注意,因为每个驱动器都有一个当前目录。join(“c:”、“foo”)表示与驱动器c: (c:foo)上当前目录相关的路径,而不是c:\foo。

#2


125  

The idea of os.path.join() is to make your program cross-platform (linux/windows/etc).

join()的思想是使程序跨平台(linux/windows/等等)。

Even one slash ruins it.

即使是一个斜杠也会毁了它。

So it only makes sense when being used with some kind of a reference point like os.environ['HOME'] or os.path.dirname(__file__).

所以只有当它被用于像os这样的引用点时才有意义。环境(“回家”)或os.path.dirname(__file__)。

#3


57  

os.path.join() can be used in conjunction with os.path.sep to create an absolute rather than relative path.

join()可以与os.path结合使用。创建绝对路径而不是相对路径。

os.path.join(os.path.sep, 'home','build','test','sandboxes',todaystr,'new_sandbox')

#4


13  

Do not use forward slashes at the beginning of path components, except when refering to the root directory:

不要在路径组件的开头使用前斜线,除非是指向根目录:

os.path.join('/home/build/test/sandboxes', todaystr, 'new_sandbox')

see also: http://docs.python.org/library/os.path.html#os.path.join

参见:http://docs.python.org/library/os.path.html # os.path.join

#5


11  

It's because your '/new_sandbox/' begins with a / and thus is assumed to be relative to the root directory. Remove the leading /.

这是因为您的“/new_sandbox/”开头是一个/,因此被假定为相对于根目录。删除/。

#6


8  

To help understand why this surprising behavior isn't entirely terrible, consider an application which accepts a config file name as an argument:

为了帮助理解为什么这种奇怪的行为并不完全是糟糕的,考虑一个接受配置文件名的应用程序作为参数:

config_root = "/etc/myapp.conf/"
file_name = os.path.join(config_root, sys.argv[1])

If the application is executed with:

如申请以下列方式执行:

$ myapp foo.conf

The config file /etc/myapp.conf/foo.conf will be used.

配置文件/etc/myapp.conf/foo.conf将使用。

But consider what happens if the application is called with:

但是考虑一下如果应用程序被调用时会发生什么:

$ myapp /some/path/bar.conf

Then myapp should use the config file at /some/path/bar.conf (and not /etc/myapp.conf/some/path/bar.conf or similar).

那么myapp应该使用/some/path/bar的配置文件。conf(而不是/etc/myapp.conf/some/path/bar.conf或类似)。

It may not be great, but I believe this is the motivation for the absolute path behaviour.

这可能不是很好,但我相信这是绝对路径行为的动机。

#7


7  

To make your function more portable, use it as such:

为了使你的功能更便于携带,可以这样使用:

os.path.join(os.sep, 'home', 'build', 'test', 'sandboxes', todaystr, 'new_sandbox')

or

os.path.join(os.environ.get("HOME"), 'test', 'sandboxes', todaystr, 'new_sandbox')

#8


4  

Try with new_sandbox only

尝试与new_sandbox只

os.path.join('/home/build/test/sandboxes/', todaystr, 'new_sandbox')

#9


3  

do it like this, without too the extra slashes

像这样吗,不要再加上额外的斜杠

root="/home"
os.path.join(root,"build","test","sandboxes",todaystr,"new_sandbox")

#10


0  

Note that a similar issue can bite you if you use os.path.join() to include an extension that already includes a dot, which is what happens automatically when you use os.path.splitext(). In this example:

注意,如果您使用os.path.join()来包含已经包含点的扩展,那么类似的问题可能会困扰您,这是使用os.path.splitext()时自动发生的情况。在这个例子中:

components = os.path.splitext(filename)
prefix = components[0]
extension = components[1]
return os.path.join("avatars", instance.username, prefix, extension)

Even though extension might be .jpg you end up with a folder named "foobar" rather than a file called "foobar.jpg". To prevent this you need to append the extension separately:

尽管扩展名可能是.jpg,但最终你会得到一个名为“foobar”的文件夹,而不是一个名为“foobar.jpg”的文件。为了防止这一点,您需要分别附加扩展:

return os.path.join("avatars", instance.username, prefix) + extension

#11


0  

Try combo of split("/") and * for strings with existing joins.

尝试使用split("/")和*组合来处理具有现有连接的字符串。

import os

home = '/home/build/test/sandboxes/'
todaystr = '042118'
new = '/new_sandbox/'

os.path.join(*home.split("/"), todaystr, *new.split("/"))


How it works...

它是如何工作的…

split("/") turns existing path into list: ['', 'home', 'build', 'test', 'sandboxes', '']

split("/")将现有路径转换为列表:["、" home "、" build "、" test "、" sandboxes "、"]

* in front of the list breaks out each item of list its own parameter

*在列表前面,列出每个列表项的参数

#1


296  

The latter strings shouldn't start with a slash. If they start with a slash, then they're considered an "absolute path" and everything before them is discarded.

后面的字符串不应该以斜线开头。如果它们以斜线开头,那么它们就被认为是“绝对路径”,在它们之前的所有东西都被丢弃。

Quoting the Python docs for os.path.join:

为os.path.join引用Python文档:

If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

如果一个组件是一个绝对路径,那么所有先前的组件都会被丢弃,并从绝对路径组件继续连接。

Note on Windows, the behaviour in relation to drive letters, which seems to have changed compared to earlier Python versions:

关于Windows的注意事项,与驱动字母相关的行为,与早期的Python版本相比似乎已经发生了变化:

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

在Windows上,当遇到绝对路径组件(例如,r'\foo')时,驱动器字母不会被重置。如果一个组件包含一个驱动器号,那么所有先前的组件都将被丢弃,驱动器号将被重置。注意,因为每个驱动器都有一个当前目录。join(“c:”、“foo”)表示与驱动器c: (c:foo)上当前目录相关的路径,而不是c:\foo。

#2


125  

The idea of os.path.join() is to make your program cross-platform (linux/windows/etc).

join()的思想是使程序跨平台(linux/windows/等等)。

Even one slash ruins it.

即使是一个斜杠也会毁了它。

So it only makes sense when being used with some kind of a reference point like os.environ['HOME'] or os.path.dirname(__file__).

所以只有当它被用于像os这样的引用点时才有意义。环境(“回家”)或os.path.dirname(__file__)。

#3


57  

os.path.join() can be used in conjunction with os.path.sep to create an absolute rather than relative path.

join()可以与os.path结合使用。创建绝对路径而不是相对路径。

os.path.join(os.path.sep, 'home','build','test','sandboxes',todaystr,'new_sandbox')

#4


13  

Do not use forward slashes at the beginning of path components, except when refering to the root directory:

不要在路径组件的开头使用前斜线,除非是指向根目录:

os.path.join('/home/build/test/sandboxes', todaystr, 'new_sandbox')

see also: http://docs.python.org/library/os.path.html#os.path.join

参见:http://docs.python.org/library/os.path.html # os.path.join

#5


11  

It's because your '/new_sandbox/' begins with a / and thus is assumed to be relative to the root directory. Remove the leading /.

这是因为您的“/new_sandbox/”开头是一个/,因此被假定为相对于根目录。删除/。

#6


8  

To help understand why this surprising behavior isn't entirely terrible, consider an application which accepts a config file name as an argument:

为了帮助理解为什么这种奇怪的行为并不完全是糟糕的,考虑一个接受配置文件名的应用程序作为参数:

config_root = "/etc/myapp.conf/"
file_name = os.path.join(config_root, sys.argv[1])

If the application is executed with:

如申请以下列方式执行:

$ myapp foo.conf

The config file /etc/myapp.conf/foo.conf will be used.

配置文件/etc/myapp.conf/foo.conf将使用。

But consider what happens if the application is called with:

但是考虑一下如果应用程序被调用时会发生什么:

$ myapp /some/path/bar.conf

Then myapp should use the config file at /some/path/bar.conf (and not /etc/myapp.conf/some/path/bar.conf or similar).

那么myapp应该使用/some/path/bar的配置文件。conf(而不是/etc/myapp.conf/some/path/bar.conf或类似)。

It may not be great, but I believe this is the motivation for the absolute path behaviour.

这可能不是很好,但我相信这是绝对路径行为的动机。

#7


7  

To make your function more portable, use it as such:

为了使你的功能更便于携带,可以这样使用:

os.path.join(os.sep, 'home', 'build', 'test', 'sandboxes', todaystr, 'new_sandbox')

or

os.path.join(os.environ.get("HOME"), 'test', 'sandboxes', todaystr, 'new_sandbox')

#8


4  

Try with new_sandbox only

尝试与new_sandbox只

os.path.join('/home/build/test/sandboxes/', todaystr, 'new_sandbox')

#9


3  

do it like this, without too the extra slashes

像这样吗,不要再加上额外的斜杠

root="/home"
os.path.join(root,"build","test","sandboxes",todaystr,"new_sandbox")

#10


0  

Note that a similar issue can bite you if you use os.path.join() to include an extension that already includes a dot, which is what happens automatically when you use os.path.splitext(). In this example:

注意,如果您使用os.path.join()来包含已经包含点的扩展,那么类似的问题可能会困扰您,这是使用os.path.splitext()时自动发生的情况。在这个例子中:

components = os.path.splitext(filename)
prefix = components[0]
extension = components[1]
return os.path.join("avatars", instance.username, prefix, extension)

Even though extension might be .jpg you end up with a folder named "foobar" rather than a file called "foobar.jpg". To prevent this you need to append the extension separately:

尽管扩展名可能是.jpg,但最终你会得到一个名为“foobar”的文件夹,而不是一个名为“foobar.jpg”的文件。为了防止这一点,您需要分别附加扩展:

return os.path.join("avatars", instance.username, prefix) + extension

#11


0  

Try combo of split("/") and * for strings with existing joins.

尝试使用split("/")和*组合来处理具有现有连接的字符串。

import os

home = '/home/build/test/sandboxes/'
todaystr = '042118'
new = '/new_sandbox/'

os.path.join(*home.split("/"), todaystr, *new.split("/"))


How it works...

它是如何工作的…

split("/") turns existing path into list: ['', 'home', 'build', 'test', 'sandboxes', '']

split("/")将现有路径转换为列表:["、" home "、" build "、" test "、" sandboxes "、"]

* in front of the list breaks out each item of list its own parameter

*在列表前面,列出每个列表项的参数