0

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?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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] 

Comments

0

This way:

 >>> str = '/home/build_targets/JavaFiles/Commonfeature/CM/src/com/ems/ClusterGroupTreePanel.java' >>> s = str.rfind('/') >>> name = str[s:] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.