打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python批量重命名文件的方法
暂无评论
用到了os的两个接口:
1、列出文件夹中的所有文件(也包含目录)
os.listdir(path)
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Availability: Unix, Windows.
Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects
2、对文件进行重命名
os.rename(src, dst)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.
Availability: Unix, Windows
1
2
3
4
5
6
7
8
9
import os
dirpath="D:/workbench/crazyant.net/myfiles"
for fname in os.listdir(dirpath):
newfname=fname[3:]
newfpath="%s/%s"%(dirpath,newfname)
oldfpath="%s/%s"%(dirpath,fname)
os.rename(oldfpath, newfpath)
其实就是用os.listdir读取里面所有的文件,然后用os.rename进行文件重命名即可实现。
python的os模块官方介绍:http://docs.python.org/2/library/os.html
转载请注明来源:http://www.crazyant.net/1397.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python shell
四十篇办公自动化文章的精华总结,代码可直接套用
批量修改文件名方法
python os模块详解
Python如何修改文件夹名称?
Python获取指定文件夹下的文件名
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服