简体   繁体   中英

How to rename the last part of file names with a list of names in excel

I am having problem renaming files using python based on a list. Say I have some files downloaded already and are listed based on when they were last modified (the order of files is important in naming), and I also have a list of names in an excel file that I would like to take and put them in the last part of file name. For example I have 10 files in a folder and the first file is named "test123.csv" and the second file as "test456", and in my excel sheet the first 3 extensions are called run, jump and swim. So I want the first file to be renamed "test123run.csv", and the second file as "test456jump.csv". Any help will be appreciated, thanks

You can try using os.rename:

use it like this:

import os
os.rename(theFileYouWantToRename, theNewFileNameYouWant) 

It accepts string and for what you are trying to achieve you might want to concatenate the old name with the extension you want to add.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM