如题,因为业务需求需要批量修改一些文件后缀,我的文件主要是音频文件以及对应的图片.每一个音频文件以及图片都在同一个文件夹内.总共有一百多个文件夹.所以为了方便也为了学习应用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)
以上内容来源于百科书,可以关注我了解更多.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/282841.html