Python Forum

Full Version: Splitting a path
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

Given these two file paths
/tmp/tmpya51Zz/Test.php
/tmp/tmpya51Zz/_unittest/TestTest.php
Please can someone tell me the most efficient way to extract
Test.php
_unittest/TestTest.php
[/python]

Thanks
in python 3.5+
import os
paths = ['/tmp/tmpya51Zz/Test.php', '/tmp/tmpya51Zz/_unittest/TestTest.php']
common = os.path.commonpath(paths)
print([p.replace(common, '') for p in paths])
otherwise you can use os.path.dirname(path) on the first path to get the common part
Thanks