国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python 文件重命名工具代碼

2020-02-23 04:43:24
字體:
來源:轉載
供稿:網友
代碼如下:
#Filename:brn.py
#Description: batch replace certain words in file names
#Use to bat rename the file in a dir(modify the suffix from a to b) for Windows Vista OS
import sys
import os
import fnmatch
import re
#parse params
p=input("Please input work directory(current path for enter):")
if p=='/r':
p='.'
p=p.rstrip('/r')
print (p)
while not os.path.exists(p):
print (p+' is not existed.Please input the work directory:')
p=input("Please input work directory(current path for enter):")
s=input("Please enter the words which need be modified(must):")
while s=='/r':
s=input("Please enter the words which need be replaced(must):")
s=s.rstrip('/r')
d=input("Please enter the words which want to change to(must):")
while d=='/r':
d=input("Please enter the words which want to change to(must):")
d=d.rstrip('/r')
try:
sure=input("Are you sure to rename the file named *"+s+"*"+" to *"+d+"*"+" in directory "+p+"? y/n:")
sure=sure.rstrip('/r')
if sure!='y':
print ("Cancel")
else:
for root, dirs, files in os.walk(p, True):
for file in files:
print (os.path.join(root,file))
if os.path.isfile(os.path.join(root,file)):#Only file is file,not a dir ,do this
if fnmatch.fnmatch(file, '*'+s+'*'):
f=str(file).replace(s,d)
if p=='.':
command='move '+str(file)+" "+f
else:
command="move "+os.path.join(root,file)+" "+os.path.join(root,f)
print (command)
if os.system(command)==0:#do actual rename
print ("Rename "+str(file)+" to "+f+" success")
else:
print ("Rename "+str(file)+" to "+f+" failed")
#else:
#print str(file)+" is a directory.omit"
except IndexError:
print (IndexError.message)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 施甸县| 成都市| 金湖县| 漳浦县| 广水市| 平顺县| 永善县| 贵阳市| 马龙县| 邹平县| 尉氏县| 高台县| 双辽市| 和顺县| 天水市| 佛学| 积石山| 浦县| 常州市| 望谟县| 邵武市| 大足县| 静海县| 安陆市| 措美县| 秦皇岛市| 沂源县| 兴和县| 南雄市| 玉树县| 新疆| 龙陵县| 赣州市| 修水县| 马鞍山市| 宿松县| 都昌县| 德化县| 婺源县| 宿松县| 炉霍县|