foo = '/input/directory/'
faa = ['/input/directory/file1.txt', '/input/directory/file2.txt']
I need to remove the directory name (i.e foo) from my list (faa) and the file extension, leaving just
bar = ['file1', 'file2']
Use map
bar = map(lambda x: '.'.join(x.replace(foo, '').split('.')[:-1]), faa)
foo = '/input/directory/'
faa = ['/input/directory/file1.txt', '/input/directory/file2.txt']
import os.path
bar = [os.path.splitext(path.replace(foo, ''))[0]
for path in faa]
print(bar)
Or without foo:
faa = ['/input/directory/file1.txt', '/input/directory/file2.txt']
from os.path import basename, splitext
bar = [splitext(basename(path))[0]
for path in faa]
print(bar)
You could do the following:
import os
foo = '/input/directory/'
faa = ['/input/directory/file1.txt', '/input/directory/file2.txt', '/not/matching/foo/file3.txt']
faa = [os.path.splitext(os.path.split(f)[1])[0] if f.startswith(foo) else f for f in faa]
print faa
This would give you the following list:
['file1', 'file2', '/not/matching/foo/file3.txt']
If you always want the filename then:
import os
foo = '/input/directory/'
faa = ['/input/directory/file1.txt', '/input/directory/file2.txt', '/not/matching/foo/file3.txt']
faa = [os.path.splitext(os.path.split(f)[1])[0] for f in faa]
print faa
Giving:
['file1', 'file2', 'file3']
You can do the following:
import re
foo = '/input/directory/'
faa = ['/input/directory/file1.txt', '/input/directory/file2.txt']
# Use a regular expression to match '<foo>someFileName<.fileExtension>'
faa = [re.sub(r'^%s(.*)\.\w+$' % foo, '\g<1>', elem) for elem in faa]
# faa => ['file1', 'file2']
foo = '/input/directory' <- the string should be quotedYou could use re module and re.findall to achieve that with positive look behind (?<=) for your foo string and positive look ahead for extension with anchor to the end of the string:
res = [re.findall('(?<={}).+(?=[.].*$)'.format(foo), elem)[0] for elem in faa]
print(res)
['file1', 'file2']
Note: You could check that regex expression with regex101.com