首页 > 编程知识 正文

批量修改文件扩展名,批量修改文件的部分名

时间:2023-05-06 21:26:53 阅读:190429 作者:2740

python复制、移动、文件和批量更改文件名1 .复制文件#示例:路径D:/我的传输文件/将/Desktop/img/中的所有. jpg格式的文件传输到路径d3360/我的传输文件复制到下面的importosimg的复制函数defmycopyfile(srcfile, dstpath ) : # srcfile复制、需要移动的文件(dstpath的目标地址ifnotOS.path.isfile ) : print (“%”) ) ) 652 ) fname=OS.path.split(srcfile )文件名和路径ifno tos.path.exists (dst path ) : OS.makedirs (dst path ) )分离为路径shutil 复制文件print('copy%s-%s ) % ) srcfile, dstpath fname ()路径是正斜杠src_dir='D:/我的传输文件/Desktop/img/'dst_dir='D:/我的传输文件/desktop/img 在此选择复制所有. JPG格式的文件forsrcfileinsrc _ file _ list : my copyfile (srcfile ),然后执行复制dst_dir文件的操作2 .将文件的移动函数defmymovefile(srcfile,dstpath ) : # srcficfiele,用于将所有. jpg格式的文件通过路径d3360/我的移动文件/Desktop/imgjpg/移动到impg (% (srcfile ) ) else: fpath, fname=OS.path.split(srcfile )文件名和路径ifno tos.path.exists (dst path ) : OS.makedirs (dst path ) )分离为路径shutil 移动文件print ) ' move%s-%s ) % ) srcfile, 在dstpath fname ()路径中输入正斜杠src_dir='D:/我的传输文件/Desktop/img/'dst_dir='D:/我的传输文件/desktop/img )。src _ file _ list=glob.glob (src _ dir ' *.jpg ) #此处为所有. jpg格式的文件forsrcfileinsrc _ glob (根据需要) 移动文件3 .“文件”名称(-(*-coding : utf-8 (*-importos )配置文件路径path='d:( ) (我的移动文件() 桌面) ) )的photo'i=1#目录下的文件的forfileinOS.listdir(path ) : # )文件ifOS.path.isfile file(==true: )新文件名new_name=file.replace ) file, ' %d.jpg'%i ) #OS.rename(OS.path.join ) path )重命名new _ name (I=1)结束打印(end )4.“文件夹”名称)配置文件路径path=(d: () ) ) ) )遍历要批量更改的文件(file'i=1#目录下的文件) )文件ifOS.path.isdir ) OS.path.Join )判断是否为path,category )==True: #设置新文件名new _ name=category.repath

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。