Home  >  Article  >  Backend Development  >  Examples to explain how to batch modify file names in Python

Examples to explain how to batch modify file names in Python

WBOY
WBOYforward
2022-06-21 12:08:273180browse

本篇文章给大家带来了关于python视频教程的相关知识,其中主要介绍了关于批量修改文件名的相关问题,包括了在原有的名字前中后批量加字、所有文件重新命名并添加序号等等内容,下面一起来看一下,希望对大家有帮助。

Examples to explain how to batch modify file names in Python

推荐学习:python

一、在原有的名字前中后批量加字

随意一点,这是我刚刚新建的文件夹和我存放的路径。
Examples to explain how to batch modify file names in Python
我们来看看代码,我都详细注释了。

import os #导入模块
filename = 'C:\\Users\\Administrator\\Desktop\\123' #文件地址
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('修改完成')

如果你照抄,原有的名字没动,这个代码只会在原有的名字后面添加你想取的名字+原有的名字。

Examples to explain how to batch modify file names in Python

如果你要在前面添加,在第八行把 + name 删了。

Examples to explain how to batch modify file names in Python

如果你要在后面添加,第八行把+ kid 删了。

Examples to explain how to batch modify file names in Python

二、所有文件重新命名并添加序号

这种的话,直接把原来的名字都给改掉,在后面添加序号,来我们先准备要改的文件。
Examples to explain how to batch modify file names in Python

import os  #导入模块

filename = 'C:\\Users\\Administrator\\Desktop\\123' #文件地址
list_path = os.listdir(filename)   #读取文件夹里面的名字

count = 1for index in list_path:
    path = filename + '\\' + index  # 原本文件名
    new_path = filename + '\\' + f'彦祖,你又来看我文章了{count}'
    print(new_path)
    os.rename(path, new_path)
    count += 1print('修改完成')

代码的话,大致跟前面差不多,没怎么注释了,就是加上序号和覆盖原本的名字。看看效果

Examples to explain how to batch modify file names in Python
当然序号的话,也可以放在后面,把 彦祖,你又来看我文章了{count}换成 {count}彦祖,你又来看我文章了 前后换一下就行了。

三、导入Excel数据批量修改为文件名

这个的话,咱们首先要有Excel数据,没有的话瞎编一个。

Examples to explain how to batch modify file names in Python
然后要改名的文件,这回我用的是文本文档,因为等下还有个小技巧。
Examples to explain how to batch modify file names in Python

代码

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 我们来试试看
Examples to explain how to batch modify file names in Python
可能有人要问了,说好的小技巧呢? 莫慌,来了来了~

你们有没有注意到我修改后的文件是不是不一样的,没得格式。

所以我们还得加上个格式,至于是什么格式,你原本的文件是什么格式就加上什么格式。

我们在新的文件位置那行最后,括号里面加上+".txt" 我这里是txt文件我就加txt了。

Examples to explain how to batch modify file names in Python

推荐学习:python视频教程

The above is the detailed content of Examples to explain how to batch modify file names in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete