Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constructing absolute path with os.path.join()

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: %s" % (os.path.join('etc', 'init.d'),)  MYPATH: etc/init.d 

Adding a dummy first-element (e.g. '') doesn't help anything:

 print("MYPATH: %s" % (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: %s" % (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: %s" % (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: %s" % (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)

like image 419
umläute Avatar asked Jul 02 '13 15:07

umläute


People also ask

What is os path join ()?

path. join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator ('/') following each non-empty part except the last path component.

Why we use os path join?

Using os. path. join makes it obvious to other people reading your code that you are working with filepaths. People can quickly scan through the code and discover it's a filepath intrinsically.

Why does os path join not work?

join function won't work if a component is an absolute path because all previous components are thrown away and joining continues from the absolute path component. The path strings shouldn't start with a slash. If they start with a slash, then they are believed an “absolute path” and everything before them is dumped.

How do I join path?

Choose Object > Path > Join. Notice that the anchor points on the left side of the paths are now joined with a path. If you want to join specific anchor points from separate paths, select the anchor points and press Command+J (Mac OS) or Ctrl+J (Windows). Choose Object > Path > Join once more.


1 Answers

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

like image 58
girardc79 Avatar answered Nov 08 '22 20:11

girardc79