Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting a path
#1
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
Reply
#2
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
Reply
#3
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,203 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,741 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020