使用python批量修改文件后缀并移动到其他目录
2022/8/30 1:24:14
本文主要是介绍使用python批量修改文件后缀并移动到其他目录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
如题,因为业务需求需要批量修改一些文件后缀,我的文件主要是音频文件以及对应的图片.每一个音频文件以及图片都在同一个文件夹内.总共有一百多个文件夹.所以为了方便也为了学习应用python,写了一个小的程序进行批量移动
代码如下
import os import os.path import shutil def demo_files(): dir_str = os.getcwd()+"\\duanshu" #原文件目录 dir_tem = os.getcwd()+'\\tem' #临时存放目录,用于存放修改后的音频 if not os.path.exists(dir_tem): os.makedirs(dir_tem) print(dir_str) files = os.listdir(dir_str)#获取目录下的所有文件名称 for file_name in files: print(file_name) #进入到对应的文件夹内 files_tem = os.listdir(dir_str+"\\"+file_name) for files_tem_name in files_tem: print(files_tem_name) print("文件名===={}".format(files_tem_name)) tem = os.path.splitext(files_tem_name) if tem[1] == '.mp4': path_tem = dir_str + "\\" + file_name + "\\" + files_tem_name #源文件 to_file = dir_tem + "\\" + tem[0] + ".mp3" #移动后的文件 print("开始移动文件=="+dir_str+"\\"+file_name+"\\"+files_tem_name+"到"+to_file) shutil.copyfile(path_tem,to_file)
以上内容来源于百科书,可以关注我了解更多.
这篇关于使用python批量修改文件后缀并移动到其他目录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python
- 2024-03-12python colon equals