python实用一技--重命名

某宝给小朋友买了个电子琴学习光盘,想放到ipad播放,但光盘上的文件为dat格式,需转为msp格式,以下为转换代码(其实就是重命名文件):

 1 #encoding=utf-8
 2 """
 3  将VCD的DAT文件命令为mpg文件
 4 """
 5 import os
 6 path = r"E:\家庭&生产\B\MPEGAV2"
 7 filelist = os.listdir(path)  
 8 count=0
 9 def getNewName(oldfile):             #旧名改新名
10     name = oldfile.split('.')[0]
11     return name + ".mpg"
12 
13 for file in filelist:
14     # print(file)
15     if "DAT" in file:
16         newname = getNewName(file)
17         old_file = os.path.join(path, file)
18         new_file = os.path.join(path, newname)
19         print("rename %s to %s" %(old_file,new_file))
20         os.rename(old_file, new_file)        #重命名核心功能函数
21     else:
22         print("not dat file")
View Code

猜你喜欢

转载自www.cnblogs.com/cwind/p/12241100.html