constructing absolute path with os.path.join()

自闭症网瘾萝莉.ら 提交于 2019-11-27 03:43:38

问题


I'd like to construct an absolute path in python, while at the same time staying fairly oblivious of things like path-separator.

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:)

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 relative paths:

 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()

 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:

 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)


回答1:


Using os.sep as root worked for me:

path.join(os.sep, 'python', 'bin')

Linux: /python/bin

Windows: \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(path.join(os.sep, 'python', 'bin'))

Linux: /python/bin

Windows: C:\python\bin




回答2:


I think you can use os.path.normpath. Here's what I get on 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?




回答3:


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



回答4:


So you can do a check for running os by sys.platfrom

on windows

>>> sys.platform
'win32'

on 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




回答5:


you could try with os.path.splitdrive to get the name of your drive/filesystem, then join this with your foo string.

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.



来源:https://stackoverflow.com/questions/17429044/constructing-absolute-path-with-os-path-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!