[英]Python os.path.join() and "+" in string concatenation
If I want to use a path "a\/b", what is the difference between os.path.join("a\/","b") and "a\/"+"b" ?如果我想使用路径“a\/b”, os.path.join("a\/","b") 和 "a\/"+"b" 有什么区别?
Like this:像这样:
The separator character is OS dependent.分隔符取决于操作系统。 If you're going to be using your script on both windows and, say, linux, you should use the
os.path.join<\/code> function.
如果您要在两个 Windows 上使用您的脚本,比如在 linux 上,您应该使用
os.path.join<\/code>函数。
It probably doesn't matter if you're going to keep your script on the same machine, but you could also use the os.sep<\/code> function to get the current system's separator character: https:\/\/docs.python.org\/3\/library\/os.html#os.sep<\/a>
如果您要将脚本保留在同一台机器上可能并不重要,但您也可以使用
os.sep<\/code>函数来获取当前系统的分隔符:
https<\/a> :\/\/docs.python.org\/3\/图书馆\/os.html#os.sep<\/a>
"
You can use os.path and its functions, which take care of OS-specific paths.您可以使用 os.path 及其函数,它们负责处理特定于操作系统的路径。 Will work on Windows with backslashes '\\' and Unix (including Mac OS X) with '\/'.
可以在带有反斜杠“\\”的 Windows 和带有“\/”的 Unix(包括 Mac OS X)上工作。
>>> import os
>>> os.path.join('root', 'subdir', 'dir', 'filename.ext')
'root/subdir/dir/filename.ext'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.