Python 使用 os.path.join() 构建绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17429044/
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
constructing absolute path with os.path.join()
提问by uml?ute
I'd like to construct an absolute path in python, while at the same time staying fairly oblivious of things like path-separator.
我想在 python 中构建一个绝对路径,同时对路径分隔符之类的东西保持相当的关注。
edit0:for instance there is a directory on the root of my filesystem /etc/init.d
(or C:\etc\init.d
on w32), and I want to construct this only from the elements etc
and init.d
(on w32, I probably also need a disk-ID, like C:
)
edit0:例如,在我的文件系统/etc/init.d
(或C:\etc\init.d
w32)的根目录上有一个目录,我只想从元素etc
和init.d
(在 w32 上,我可能还需要一个磁盘 ID,例如C:
)
In order to not having to worry about path-separators, os.join.path()
is obviously the tool of choice. But it seems that this will only ever create relativepaths:
为了不必担心路径分隔符,os.join.path()
显然是首选工具。但这似乎只会创建相对路径:
print "MYPATH:", os.path.join('etc', 'init.d')
MYPATH: etc/init.d
Adding a dummy first-element (e.g. ''
) doesn't help anything:
添加一个虚拟的第一个元素(例如''
)没有任何帮助:
print "MYPATH:", os.path.join('', 'etc', 'init.d')
MYPATH: etc/init.d
Making the first element absolute obviously helps, but this kind of defeats the idea of using os.path.join()
使第一个元素绝对显然有帮助,但这种方法会破坏使用的想法 os.path.join()
print "MYPATH:", os.path.join('/etc', 'init.d')
MYPATH: /etc/init.d
edit1:using os.path.abspath()
will only try to convert a relative path into an absolute path.
e.g. consider running the following in the working directory /home/foo
:
edit1:usingos.path.abspath()
只会尝试将相对路径转换为绝对路径。例如,考虑在工作目录中运行以下内容/home/foo
:
print "MYPATH:", os.path.abspath(os.path.join('etc', 'init.d'))
MYPATH: /home/foo/etc/init.d
So, what is the standard cross-platform way to "root" a path?
那么,“根”路径的标准跨平台方式是什么?
root = ??? # <--
print "MYPATH:", os.path.join(root, 'etc', 'init.d')
MYPATH: /etc/init.d
edit2:the question really boils down to: since the leading slash in /etc/init.d
makes this path an absolute path, is there a way to construct this leading slash programmatically?
(I do not want to make assumptions that a leading slash indicates an absolute path)
编辑2:问题实际上归结为:由于前导斜杠/etc/init.d
使这条路径成为绝对路径,有没有办法以编程方式构造这条前导斜杠?(我不想假设前导斜杠表示绝对路径)
采纳答案by uml?ute
so the solution i came up with, is to construct the root of the filesystem by following a given file to it's root:
所以我想出的解决方案是通过跟随给定文件到它的根来构建文件系统的根:
def getRoot(file=None):
if file is None:
file='.'
me=os.path.abspath(file)
drive,path=os.path.splitdrive(me)
while 1:
path,folder=os.path.split(path)
if not folder:
break
return drive+path
os.path.join(getRoot(), 'etc', 'init.d')
回答by Ansuman Bebarta
So you can do a check for running os by sys.platfrom
所以你可以通过 sys.platfrom 检查运行 os
on windows
在窗户上
>>> sys.platform
'win32'
on linux
在 linux 上
>>> sys.platform
'linux2'
then
然后
if sys.platform == 'win32':
ROOT = os.path.splitdrive(os.path.abspath('.'))[0]
elif sys.platform == 'linux2':
ROOT = os.sep
Please note that 'linux2' may not cover all linux distros
请注意,“linux2”可能无法涵盖所有 linux 发行版
回答by Christoph
you could try with os.path.splitdrive
to get the name of your drive/filesystem, then join this with your foo
string.
您可以尝试os.path.splitdrive
获取驱动器/文件系统的名称,然后将其与您的foo
字符串连接。
http://docs.python.org/2/library/os.path.html#os.path.splitdrive
http://docs.python.org/2/library/os.path.html#os.path.splitdrive
something like (untested!)
类似(未经测试!)
(drive, tail) = os.path.splitdrive(os.getcwd())
os.path.join(drive, 'foo')
should do the trick.
应该做的伎俩。
回答by Hyman O'Connor
I think you can use os.path.normpath
. Here's what I get on Windows:
我认为你可以使用os.path.normpath
. 这是我在 Windows 上得到的:
>>> os.path.normpath("/etc/init.d")
'\etc\init.d'
I'm not sure exactly what the right thing to do with the drive prefix is, but I think leaving it off means something like "keep using the drive I'm on now," which is probably what you want. Maybe someone more familiar with Windows can clarify?
我不确定使用驱动器前缀做什么正确,但我认为不使用它意味着“继续使用我现在使用的驱动器”,这可能是您想要的。也许更熟悉Windows的人可以澄清一下?
回答by girardc79
Using os.sep
as root worked for me:
os.sep
以 root 身份使用对我有用:
path.join(os.sep, 'python', 'bin')
Linux: /python/bin
Linux: /python/bin
Windows: \python\bin
视窗: \python\bin
Adding path.abspath()
to the mix will give you drive letters on Windows as well and is still compatible with Linux:
添加path.abspath()
到组合中也会为您提供 Windows 上的驱动器号,并且仍然与 Linux 兼容:
path.abspath(path.join(os.sep, 'python', 'bin'))
Linux: /python/bin
Linux: /python/bin
Windows: C:\python\bin
视窗: C:\python\bin