|
|
国庆长假,大家肯定都照了好多照片,是不是觉得照片命名看着很不爽呢,初学python,写了个文件重命名程序,前缀+编号
- #!\usr\bin\env python
- import os
- import shutil
- # 判断是否是路径,以及是否存在
- def checkDir(dirPath):
- if not os.path.isdir(dirPath):
- return 0
- else:
- return 1
- # 验证输入的字符是否是合法文件名函数
- # 文件名不能包含下载任何字符之一: \/:*?"<>|
- def checkName(qianzhui):
- signsstr = '\/:*?"<>|'
- for sign in signsstr:
- if sign in qianzhui:
- return 0
- else:
- return 1
- #主程序开始
- print "重命名后所有文件以前缀加数字编号为文件名"
- while True:
- dirPath = raw_input("目录(本目录时可直接回车):")
- if dirPath=='':
- dirPath='.'
- if not checkDir(dirPath):
- print "输入的路径不正确!"
- continue
- else:
- break
- print (dirPath)
- while True:
- qianzhui = raw_input("前缀{无前缀时直接回车}:")
- if not checkName(qianzhui):
- print "输入的前缀不合法!"
- continue
- else:
- break
- dir = dirPath
- global x
- x=0
- for i in os.listdir(dir):
- if i=='renameFiles.exe' or i=='renameFiles.py':
- continue
- else:
- x=x+1
- print x
- oldname = dir+'\\'+i
- newname = dir+'\\'+qianzhui+''+str(x)+ '.'+i.split(".")[1]
- os.rename(oldname,newname)
- print newname
-
- print 'well done'
复制代码 电脑安装python的话,直接保存为renameFiles.py文件,cmd后 python renameFiles.py
windows下的话,可以用py2exe把python程序编译成exe,在没安装python的机器运行.Linux默认都安装了python |
|