I have files that look like this.
FFIEC CDR Call Schedule RCCI 03312001.txt
FFIEC CDR Call Schedule RCCI 03312002.txt
FFIEC CDR Call Schedule RCCI 03312003.txt
etc.
FFIEC CDR Call Schedule RCCII 03312001.txt
FFIEC CDR Call Schedule RCCII 03312002.txt
FFIEC CDR Call Schedule RCCII 03312003.txt
(2 of 2)
I would like to end up with this.
FFIEC CDR Call Schedule RCC1 03312001.txt
FFIEC CDR Call Schedule RCC1 03312002.txt
FFIEC CDR Call Schedule RCC1 03312003.txt
etc.
FFIEC CDR Call Schedule RCC2 03312001.txt
FFIEC CDR Call Schedule RCC2 03312002.txt
FFIEC CDR Call Schedule RCC2 03312003.txt
Here is the code that I am testing.
# rename certain filenames
import os
import glob
path = 'C:\Users\ryans\Downloads\'
all_files = glob.glob(os.path.join(path, "*.txt"))
# first list
before = [
'FFIEC CDR Call Schedule RCCI',
'FFIEC CDR Call Schedule RCCII',
'FFIEC CDR Call Schedule RCEI',
'FFIEC CDR Call Schedule RCEII'
]
# second list
after = [
'FFIEC CDR Call Schedule RCC1',
'FFIEC CDR Call Schedule RCC2',
'FFIEC CDR Call Schedule RCE1',
'FFIEC CDR Call Schedule RCE2'
]
for f in all_files:
for x in before:
if x in f:
print(x)
print(f)
os.rename(x, path + after)
question from:
https://stackoverflow.com/questions/65546454/how-can-we-loop-through-all-file-names-in-a-folder-and-chagce-only-some-of-the-n 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…