使用Python批量修改文件名的代码实例
作者:Python新世界 时间:2022-03-21 04:02:53
这两天在整理一些文章,但是文件夹中每个文章没有序号会看起来很乱,所以想着能不能用Python写一个小脚本。
于是乎,参考了多方资料,简单写了下面几行代码
import osdef tekan():
i=1 #为序号赋初值
for old_file in os.listdir('.'): #os.listfir('.')用于获取当前文件夹所有文件名,'.'表示当前文件夹,也可改为目标文件路径
if 'py' not in old_file: #由于脚本文件不需要修改文件名,所以这里做个判断
#new_name=old_file.split(' ')[1] #这行用来将文件名回到原来状态
new_name=str(i)+' '+str(old_file) #在文件名前加上序号与空格
os.rename(old_file,new_name) #os.rename()用来修改名称
i+=1 #序号加1if __name__ == '__main__':
tekan()
接下看看看执行效果
来源:https://blog.csdn.net/qq_41841569/article/details/86571279
标签:python,批量修改,文件名
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CSS网页设计时关于字体大小的设计
2008-10-23 13:42:00
Python 操作 PostgreSQL 数据库示例【连接、增删改查等】
2021-12-14 00:54:08
python查询MySQL将数据写入Excel
2023-12-15 13:48:11
![](https://img.aspxhome.com/file/2023/8/70868_0s.png)
基于Python2、Python3中reload()的不同用法介绍
2023-10-01 17:59:15
利用python进行文件操作
2022-01-09 10:49:51
DW实现鼠标滑过切换图片
2008-02-03 18:49:00
![](https://img.aspxhome.com/file/UploadPic/20082/3/200823185431657s.jpg)
基于scrapy的redis安装和配置方法
2022-07-15 17:26:56
Windows Server 2008之数据安全保护
2008-12-04 12:49:00
PHP利用header跳转失效的解决方法
2023-09-07 23:39:45
PHP中SESSION使用中的一点经验总结
2023-11-19 11:48:54
![](https://img.aspxhome.com/file/2023/8/98088_0s.jpg)
如何在小空间放置大图片
2009-08-04 13:04:00
![](https://img.aspxhome.com/file/UploadPic/20098/4/01-47s.jpg)
PHP基于非递归算法实现先序、中序及后序遍历二叉树操作示例
2023-08-16 04:46:47
![](https://img.aspxhome.com/file/2023/7/55387_0s.jpg)
python中matplotlib实现随鼠标滑动自动标注代码
2023-09-02 10:49:47
![](https://img.aspxhome.com/file/2023/7/62867_0s.gif)
不用加载Include文件也能生成选择列表吗?
2009-10-29 12:30:00
js中鼠标滚轮事件详解
2010-02-05 12:20:00
![](https://img.aspxhome.com/file/UploadPic/20102/5/wheelchrome-82s.png)
从外到内提高SQL Server数据库性能
2009-01-22 14:12:00
![](https://img.aspxhome.com/file/UploadPic/20091/2009122172131354.jpg)
MySQL十条特殊技巧
2007-09-17 12:33:00
PHP实现数组根据某个字段进行水平合并,横向合并案例分析
2023-10-04 04:55:53
基于Django框架的权限组件rbac实例讲解
2022-09-27 17:11:51
![](https://img.aspxhome.com/file/2023/2/72032_0s.jpg)
用Python创建简易网站图文教程
2023-03-09 20:37:57
![](https://img.aspxhome.com/file/2023/0/63980_0s.png)