Hello everyone! I am a red panda ❤
As a migrant worker who is diligent (ai) diligent (hao) conscientious (mo) conscientious (yu)
Occasionally need to organize folder data
Folder that's called a clutter
It's a waste of time to modify one by one,
Today, I will share a little trick: batch modification of file names
1. Add words in batches before and after the original name
Feel free to,
This is the folder I just created and the path where I put it.
Let's take a look at the code, I've commented it out in detail.
import os #导入模块
filename = 'C:\\Users\\Administrator\\Desktop\\123' #文件地址#python学习裙:660193417#
list_path = os.listdir(filename) #读取文件夹里面的名字
for index in list_path: #list_path返回的是一个列表 通过for循环遍历提取元素
name = index.split('.')[0] #split字符串分割的方法 , 分割之后是返回的列表 索引取第一个元素[0]
kid = index.split('.')[-1] #[-1] 取最后一个
path = filename + '\\' + index
new_path = filename + '\\' + name + '彦祖你来了啊' + '.' + kid
os.rename(path, new_path) #重新命名
print('修改完成')
If you copy, the original name is not changed,
This code will only add the name you want + the original name after the original name.
If you want to add it in front, delete the + name on the eighth line.
If you want to add it later, delete the + kid on the eighth line.
2. Rename all files and add serial numbers
In this case, directly change the original name,
add the serial number at the back, and let's prepare the file to be changed first.
import os #导入模块
filename = 'C:\\Users\\Administrator\\Desktop\\123' #文件地址
list_path = os.listdir(filename) #读取文件夹里面的名字
count = 1
for index in list_path:
path = filename + '\\' + index # 原本文件名
new_path = filename + '\\' + f'彦祖,你又来看我文章了{
count}'
print(new_path)
os.rename(path, new_path)
count += 1
print('修改完成')
The code is roughly the same as before, without much commenting,
just adding the serial number and overwriting the original name.
see the effect
Of course, the serial number can also be placed in the back,
just彦祖,你又来看我文章了{count}
replace{count}彦祖,你又来看我文章了
front and back with.
3. Import Excel data to batch modify the file name
In this case, we must first have Excel data, if not, make up one.
Then the file to be renamed, this time I used a text document, because there is a little trick later.
code
import os
import xlrd
count = 1
path = "C:\\Users\\Administrator\\Desktop\\123" #文件所在文件夹
expath = "C:\\Users\\Administrator\\Desktop\\18.xls"#Excel表所在文件夹
x1 = xlrd.open_workbook(expath)#读取excel
sheet1 = x1.sheet_by_name("Sheet1")#读取sheet1
idlist = sheet1.col_values(0)#存放第一列
xylist = sheet1.col_values(1)#存放第二列
filelist = os.listdir(path)#读取文件目录
for files in filelist:#遍历文件目录
Olddir = os.path.join(path,files)#旧的文件位置
os.renames(Olddir,os.path.join(path,str(int(idlist[count]))+" "+xylist[count]))#新的文件位置
count = count +1#计数指针后移
OK, let's try it out
Some people may ask, what about the good tricks? Don't panic, here we come~
Have you noticed that my modified file is not the same and has no format.
So we have to add a format. As for the format, you can add whatever format your original file is.
We add +".txt" in the brackets at the end of the line of the new file location. Here is a txt file, so I will add txt.
I should like it here~
Today's article is here~