Python batch move documents and modify document extension

import os
import shutil
files = os.listdir(".")#获取当前目录下的文件
old_path = r'C:/Users/74865/Desktop/1'    #原文件位置
new_path = r'C:/Users/74865/Desktop/1/txt'    #目标位置


#移动文件
for filename in files:
    portion = os.path.splitext(filename)#将文件名拆成名字和后缀
    if portion[1] == ".fbd":#关于后缀 
        old_name = old_path +'/' + filename
        new_name = new_path + '/' + filename
        shutil.copy(old_name, new_name)
  
#改文件名后缀  
files = os.listdir(".")#获取当前目录下的文件
for filename in files:
    portion = os.path.splitext(filename)#将文件名拆成名字和后缀
    if portion[1] == ".fbd":#关于后缀 
        newname = portion[0] + ".txt"
        os.rename(filename, newname)
        print(filename)

 

Published 35 original articles · won praise 26 · views 80000 +

Guess you like

Origin blog.csdn.net/weixin_42342968/article/details/104553055