Python rename all files in a folder from a reference file.

Page Tags
file
rename
folder
python

Python to rename file in a folder occurding to a tab seperated settings file.

rename_files.py

#!/usr/bin/python2

import os

zFile = open('rename_from_to.txt')
zLines = zFile.readlines()
zFileMap = {}
for currentLine in zLines:
    zFileSettings = currentLine.split("\t")
    zOldFileName = zFileSettings[0]
    zFileMap[zOldFileName] = zFileSettings[1].strip()
#     print zOldFileName, zFileSettings[1]

zDir = "D:\\ses\\nasser-al-qatami\\"
for zFile in os.listdir(zDir):
    zOldFileName, zFileExtension = os.path.splitext(zFile)
    zNewFileName = zFileMap[zOldFileName]
    zOldFilePath = zDir + zOldFileName + zFileExtension
    zNewFilePath = zDir + zNewFileName + zFileExtension
    print zOldFilePath, zNewFilePath
    os.rename(zOldFilePath, zNewFilePath)

rename_from_to.txt

001    001_Fatiha
002    002_Bakara
003    003_Al-iImran
004    004_Nisa
005    005_Maide
006    006_Enam
007    007_Araf
008    008_Enfal

 


Created Wed, Aug 3, 2016 8:55 PM by Ahmet Faruk Bişkinler
Last Updated Wed, Aug 3, 2016 9:00 PM by Ahmet Faruk Bişkinler