Reputation: 2070
I have a lot of strings like this
/home/build_targets/JavaFiles/Commonfeature/CM/src/com/ems/ClusterGroupTreePanel.java
I need to get the file name ClusterGroupTreePanel.java out.
Is there an efficient way to achieve this in Python?
Upvotes: 0
Views: 78
Reputation: 3481
This way:
>>> str = '/home/build_targets/JavaFiles/Commonfeature/CM/src/com/ems/ClusterGroupTreePanel.java'
>>> s = str.rfind('/')
>>> name = str[s:]
Upvotes: 0
Reputation: 6921
os.path.basename(path) will give you the file name, if you want to leave it out you can use os.path.split and take the first part
os.path.split(full_path)[0]
Upvotes: 1
Reputation: 309841
use os.path.basename
>>> import os.path
>>> os.path.basename('/home/build_targets/JavaFiles/Commonfeature/CM/src/com/ems/ClusterGroupTreePanel.java')
'ClusterGroupTreePanel.java'
Generally, if your question goes something like "How do I manipulate this filesystem path name in an interesting way?". The answer is usually "use some combination of functions in the os.path
module". There's lots of great stuff in there and it would be well worth your time to familiarize yourself with the different functions.
Upvotes: 4