CSV File List – Rename Files in Folder Using Python
This is a simple code snippet that I developed for one my recent project.
The current name of list of files in a folder is given in a CSV file. Also the new name that each file has to be renamed is given in next column.
The Python code in this page will copy the files to a new folder. And also renames each files as per the specification in the CSV file.
Python Code to Copy Files to New Folder and RenamePython To Copy and Rename List of Files in FolderPython123456789101112131415161718192021222324import shutil, osimport pandas as pd#Set Src & Dest Pathsrc_path = 'D:/folder/'dest_path = 'D:/Newfolder/'#Read from CSV Filedf = pd.read_csv('nameslist.csv')images = df.oldnamenImages = df.newname#Make Folder if does not existos.makedirs(dest_path ,exist_ok=True)#Loop Thru each Image Filei = 0for img in images:print(str(img) + ' --> ' + nImages[i])shutil.copy( src_path + str(img) + '.jpg',dest_path)os.rename (dest_path + str(img) + '.jpg' , dest_path + nImages[i] + '.jpg' )i = i + 1print('Done!')
For renaming shutil is used and to read csv file pandas library is used.
This is just a simple and straight forward coding. That loops thru each row in CSV and processes the files one by one. Pretty much self explanatory.
Just create the csv with two column headers.
So that the values in these columns gets populated correctly in the Python list.
Previous Post: Enable Cut/Copy/Insert/Delete Options in Excel Right Click
Next Post: Excel VBA Set & Read System Environment Variables
Python – Copy , Rename List of Files in Folder
- Block Apps in FaceBook print screen Excel Countif Record Macro Timer in Excel Task Manager VCF HTML Table Import Data Thumbnails Text to Speech TTS Candy Crush C++ Programing Machine learning AI bulk email Excel VBA vba color index vba color codes Android social media Twitter Google+ Google Adsense Tips bitcoin Calendar in Excel outlook vba mass email Data Mining Excel Macro Facebook WhatsApp python Wordpress