首页 > 编程语言 >Python批量重命名文件的方法

Python批量重命名文件的方法

时间:2023-02-06 16:07:06浏览次数:50  
标签:重命名 批量 Python dst will Windows Unix dirpath os


用到了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


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​


标签:重命名,批量,Python,dst,will,Windows,Unix,dirpath,os
From: https://blog.51cto.com/peishuai/6039294

相关文章

  • Hive使用TRANSFORM运行Python脚本总结
    1、Python环境设置可以使用addcachearchive的方法把tar.gz添加到分布式缓存,Hive会自动解压压缩包,但是目录名是和压缩包名称一样的;addcachearchive${env:my_workbenc......
  • python爬取网站指定数据并存入excel
    1:安装库pipinstallbeautifulsoup4pipinstallpandas2:爬取数据我们拿 https://cuiqingcai.com/archives/ 网站为例子,来进行爬取文章标题importrequestsfrom......
  • python之路64 drf从入门到成神 9个视图子类 视图集、ModelViewSet、ReadOnlyModelV
    视图视图View两个视图基类:APIViewGenericAPIViewAPIView执行流程:新的reqeust,三大认证,全局异常重写了as_view,dispatch类属性:p......
  • python新手常见问题一:乱用表达式
    在函数参数中乱用表达式作为默认值Python允许给一个函数的某个参数设置默认值以使该参数成为一个可选参数。尽管这是这门语言很棒的一个功能,但是这当这个默认值是可变对象(mu......
  • Python新手常见问题二:不正确的使用类变量
    不正确的使用类变量看下面一个例子:>>>classA(object):...x=1...>>>classB(A):...pass...>>>classC(A):...pass...>>>printA.x,B.x,C.x111看起......
  • Python新手常见问题三:在异常处理时错误的使用参数
    在异常处理时错误的使用参数假设你有如下的代码:>>>try:...l=["a","b"]...int(l[2])...exceptValueError,IndexError:#想捕捉两个异常...pass...Tr......
  • Python实现的简易FTP
    Python版本实现了比之前的xxftp更多更完善的功能1、继续支持多用户2、继续支持虚拟目录3、增加支持用户根目录以及映射虚拟目录的权限设置4、增加支持限制用户根目录或者虚......
  • Python中and、or用法实例
    Python中and、or是Python中的逻辑运算符,它们的用法如何呢?and:在Python中,and和or执行布尔逻辑演算,如你所期待的一样,但是它们并不返回布尔值;而是,返回它们实际进行比较的值......
  • uniapp文件复制,重命名以及删除
    查找某目录下的文件plus.io.resolveLocalFileSystemURL(      "_www/static/本地.png",         function(entry){//获取文件对象       ......
  • Python工具箱系列(二十五)
    Redis是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。从......