[Tutor] Another way to strip?

ad^2 adsquaired at gmail.com
Thu Jan 19 16:29:16 EST 2017


Hello all,

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',
'/var/tmp/02_80_5302_4444_2017_01_19_08-40-07_2536_0.zip',
'/var/tmp/02_80_5302_4444_2017_01_19_09-03-30_8831_0.zip',
'/var/tmp/02_80_5303_4444_2017_01_19_08-39-42_8677_0.zip',
'/var/tmp/02_80_5308_4444_2017_01_19_08-39-06_7600_0.zip',
'/var/tmp/02_80_5305_4444_2017_01_19_08-37-24_4699_0.zip',
'/var/tmp/02_80_5309_4444_2017_01_19_08-40-30_4205_1.zip',
'/var/tmp/02_80_5304_4444_2017_01_19_08-40-55_8450_0.zip',
'/var/tmp/02_80_5301_4444_2017_01_19_08-38-22_6999_0.zip',
'/var/tmp/02_80_5307_4444_2017_01_19_08-38-09_1763_0.zip']

>>> for i in my_list:
...     i.strip('/var/tmp/.zip')

'02_80_5306_4444_2017_01_19_08-36-57_4918_0'
'02_80_5309_4444_2017_01_19_08-40-30_4205_0'
'02_80_5302_4444_2017_01_19_08-40-07_2536_0'
'02_80_5302_4444_2017_01_19_09-03-30_8831_0'
'02_80_5303_4444_2017_01_19_08-39-42_8677_0'
'02_80_5308_4444_2017_01_19_08-39-06_7600_0'
'02_80_5305_4444_2017_01_19_08-37-24_4699_0'
'02_80_5309_4444_2017_01_19_08-40-30_4205_1'
'02_80_5304_4444_2017_01_19_08-40-55_8450_0'
'02_80_5301_4444_2017_01_19_08-38-22_6999_0'
'02_80_5307_4444_2017_01_19_08-38-09_1763_0'

or in the case the directory may change in the future I can use variables
somewhere.

>>> path = '/var/tmp/'
>>> extension = '.zip'

for i in my_list:
...     i.strip(path + extension)
...
'02_80_5306_4444_2017_01_19_08-36-57_4918_0'
'02_80_5309_4444_2017_01_19_08-40-30_4205_0'
.........
.......
......
...

The results are what I need but is there another way to perform the same
thing?

Thanks,

ad^2


More information about the Tutor mailing list