3

How can I get the most left part of relative path in python?

I started with something like this:

/var/tmp/workdir/1/foo/bar/test.jpg

Then I removed some of it to get:

1/foo/bar/test.jpg

Using:

rel_path = os.path.relpath(path,base_dir)

Now how can I get the most left part - the "1" ?

I can only find tools that go from the right side, but in this case I want the most left thing because it correspondents with a user ID. Also I want to avoid going from the right side, because there might be more sub directories.

1
  • You can use regular expression or if the id is always on the same place you can split by '/' and get the wanted element. Commented Oct 12, 2014 at 18:56

3 Answers 3

5

Using str.split might give incorrect results if names contain os.path.sep (escaped of course). The safest solution imho is:

basename = None # guards against UnboundLocalError in case of empty rel_path
while rel_path:
    rel_path, basename = os.path.split(rel_path)
print basename # this will be the leftmost component
Sign up to request clarification or add additional context in comments.

2 Comments

I understand that the least thing I want to do is use split commands to manually get the first part. But why is there no command to just get the first part? I guess your approach will do, but it's a waste of cpu isn't it?
I don't think you should worry about cpu: 100000 loops, best of 3: 4.39 µs per loop. Unless you are expecting to parse thousands of paths pers second, each hundreds of components...
2

poke mentioned the pathlib library that's built into Python 3.4. You can also use pathlib on Python 2.6 or 2.7 by running pip install pathlib, as stated here: http://pathlib.readthedocs.org/en/pep428.

Your code would look like this:

>>> from pathlib import PurePath
>>> p = PurePath('1/foo/bar/test.jpg')
>>> p.parts
('1', 'foo', 'bar', 'test.jpg')

And use p.parts[0] to get the part you want.

In fact, you could do the whole thing with pathlib as follows:

>>> from pathlib import PurePath
>>> p = PurePath('/var/tmp/workdir/1/foo/bar/test.jpg')
>>> p = p.relative_to('/var/tmp/workdir')
>>> p.parts
('1', 'foo', 'bar', 'test.jpg')

1 Comment

Thank you. I will consider this, too. But I think for now I will go with the while loop, because this way I don't need extra libraries. But if I encounter more of that I will switch to your solution. Thanks again!
0

If you use Python 3.4, you could use the new pathlib for this.

Otherwise, you could just use normal string manipulation to get that part, for example with str.partition:

>>> path = '1/foo/bar/test.jpg'
>>> path.partition('/')
('1', '/', 'foo/bar/test.jpg')

You could also use os.sep instead of '/' if you want to split on the operating system’s path separator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.