为什么 os.path.join() 在这种情况下不起作用? [英] Why doesn't os.path.join() work in this case?

查看:48
本文介绍了为什么 os.path.join() 在这种情况下不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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/')

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

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

推荐答案

后面的字符串不应以斜线开头.如果它们以斜线开头,则它们被视为绝对路径",并且它们之前的所有内容都将被丢弃.

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.

引用 os.path 的 Python 文档.加入:

如果一个组件是绝对路径,则之前的所有组件都将被丢弃,并从绝对路径组件继续连接.

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

注意在 Windows 上,与驱动器号相关的行为,与早期的 Python 版本相比似乎发生了变化:

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

在 Windows 上,当遇到绝对路径组件(例如,r'foo')时,不会重置驱动器号.如果一个组件包含驱动器号,则所有以前的组件都将被丢弃并重置驱动器号.请注意,由于每个驱动器都有一个当前目录,os.path.join("c:", "foo") 表示相对于驱动器 C:<上的当前目录的路径/code> (c:foo),而不是 c:foo.

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.

这篇关于为什么 os.path.join() 在这种情况下不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆