I am trying to replace parts of file extensions in a list of files. I would like to be able to loop through items (files), and remove the extensions. I don\'t know how to ap
You can use a list comprehension to construct the new list with the cleaned up files names. \d
is the regex to match a single character and $
only matches at the end of the string.
file_lst_trimmed = [re.sub(r'\d\.fa$', '', file) for file in file_lst]
The results:
>>> file_lst_trimmed
['cats', 'cats', 'dog', 'dog']