Python版RenameURLCode
2015-08-11 15:23
561 查看
学习了几天Python之后,为了巩固所学知识,使用Python写了一个转换URL字符文件的脚本,之前用JavaScript写过一个放在SAE上(RenameURLCode),但一直希望有一个客户端的工具。
# coding:utf-8 import os import re needToTransform = False pathName = raw_input('Enter a directory name: ') extName = raw_input('file extension: ') fileList = os.listdir(pathName) reg = r'.*\.'+extName encodedURL = ['%5B', '%5D'] decodedURL = ['[', ']'] #REVERSE # encodedURL = ['[', ']'] # decodedURL = ['%5B', '%5D'] finalFileName = [] originFileName = [] for fileName in fileList: #列出指定目录下所有文件 if re.match(reg, fileName, re.I): #列出所有PDF文件 for encodeIndex in encodedURL: #循环判断单个PDF文件名是否包含URL字符 if(fileName.find(encodeIndex) != -1): #列出所有包含URL字符的PDF文件 originFileName.append(fileName) chIndex = 0 for encodeStrIndex in encodedURL: fileName = fileName.replace(encodeStrIndex, decodedURL[chIndex]) chIndex+=1 finalFileName.append(fileName) break if len(originFileName) > 0: needToTransform = True print unicode('\n\t原始文件名:', 'utf8') for i in originFileName: print i print '\n'+'='*100+'\n' print unicode('\t转换文件名:', 'utf8') for i in finalFileName: print i else: print unicode('没有文件需要转换!', 'utf8') while needToTransform: print '\n' confirm = raw_input('Sure to change file names as below? (Y/N): ') if(confirm == 'y' or confirm == 'Y'): fileIndex = 0 for originFileIndex in originFileName: os.rename(pathName+'\\'+originFileIndex, pathName+'\\'+finalFileName[fileIndex]) fileIndex+=1 break elif(confirm == 'n' or confirm == 'N'): breakURLDecoder.py
相关文章推荐
- Python中逗号的作用
- Python安装模块
- ★★★★[转载]Python学习笔记一:数据类型转换★★★★
- Python 加密 md5
- python---urllib模块
- python读取leveldb数据
- Python入门 切片
- python---内建函数学习
- Python入门 函数
- Beginning Python From Novice to Professional (7) - 类别
- 人脸识别(OpenCV+Python)
- python+opencv之视频人脸识别
- Python-OpenCV人脸检测(代码)
- python脚本发送报警邮件
- 利用Python PIL、cPickle读取和保存图像数据库
- 【机器学习算法实现】主成分分析(PCA)——基于python+numpy
- Python机器学习库
- pythonchallenge(一)
- 从Theano到Lasagne:基于Python的深度学习的框架和库
- 76 python.crawler