[Tutor] Another way to strip?

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jan 19 19:40:46 EST 2017

On 19/01/17 21:29, ad^2 wrote:

> I'm looking for a cleaner more elegant way to achieve stripping out a file
> name from a list which contains a path and file extension.
> Ex.
> my_list = ['/var/tmp/02_80_5306_4444_2017_01_19_08-36-57_4918_0.zip',
> '/var/tmp/02_80_5309_4444_2017_01_19_08-40-30_4205_0.zip',

Take a look at the os.path module. It has lots of utility functions
for splitting paths into their various bits and as a bonus it caters for
all the different OS path formats too. Its likely to be both
easier to use and more reliable than trying to use string functions
on the path.

In your case os.path.basename() is likely to be the best option.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list