3
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']

7 Answers 7

4

Use map

bar = map(lambda x: '.'.join(x.replace(foo, '').split('.')[:-1]), faa)
Sign up to request clarification or add additional context in comments.

2 Comments

This is slightly incorrect - what if the file contains a dot in its name? The first lambda should thus be modified. Edit: with the new shortened code - the slice of the split() should not be just [0] :) ... Edit 2: you should probably use rsplit(___, 1)[0] instead.
@plamut Agreed, fixed now
1

try;

>>> [x.replace(foo, '').split('.')[0] for x in faa]
['file1', 'file2']

Comments

1

maybe:

bar = [elem.replace(foo, '').rsplit('.', 1)[0] for elem in faa]

Comments

1
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)

Comments

0

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']

Comments

0

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']

2 Comments

foo = '/input/directory' <- the string should be quoted
Ah yes, you're right (I copy-pasted it from the OP's question).
0

You 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.