I am trying to rename a set of 15,000 txt-files. In seperate txt-file I do have a list of the old names of these files and the new names I want them have. But not all of the 15,000 txt-files are listed in the name-list. That is where the problem starts. How do I change my code that it ignores/skips files that are not listed in the name list or those that are duplicates? The codes works fine until it reaches one of these file. Any suggestions? Thanks! This is the code I have so far:
import os
with open("rename3.txt") as fd:
for line in fd:
line = line.strip()
if len(line) == 0: continue
old, new = line.strip().split(",", 1)
os.rename(old.strip() + ".txt", new.strip() + ".txt")
Aucun commentaire:
Enregistrer un commentaire