I have a list of file like the following
'TRIAL_20134_75690_TOTAL_2018-08-12-17-18.csv'
I want to rename them the parte after the last underscore, such as the file will be renamed like:
'TRIAL_20134_75690_TOTAL.csv'
I have a list of file like the following
'TRIAL_20134_75690_TOTAL_2018-08-12-17-18.csv'
I want to rename them the parte after the last underscore, such as the file will be renamed like:
'TRIAL_20134_75690_TOTAL.csv'
Use os.rename
from the os
library to do the renaming.
To get the sting up to the last index of an underscore (_
), use the rindex
method of string
.
You also need to concatenate the extension back on.
I.e.
import os
f = 'TRIAL_20134_75690_TOTAL_2018-08-12-17-18.csv'
os.rename(f, f[:f.rindex('_'] + '.csv')