Python 文件重命名工具代碼
更新時(shí)間:2009年07月26日 09:46:19 作者:
Python 文件重命名工具實(shí)現(xiàn)代碼。
復(fù)制代碼 代碼如下:
#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)
相關(guān)文章
Python?FastAPI?Sanic?Tornado?與Golang?Gin性能實(shí)戰(zhàn)對(duì)比
本文將深入比較Python的FastAPI、Sanic、Tornado以及Golang的Gin框架的各種特性、性能表現(xiàn)以及適用場(chǎng)景,通過詳實(shí)的性能測(cè)試和實(shí)際示例代碼,將探討它們?cè)跇?gòu)建現(xiàn)代高性能應(yīng)用中的優(yōu)劣勢(shì),以便開發(fā)者根據(jù)需求做出明智的選擇2024-01-01Python使用sklearn庫實(shí)現(xiàn)的各種分類算法簡單應(yīng)用小結(jié)
這篇文章主要介紹了Python使用sklearn庫實(shí)現(xiàn)的各種分類算法,結(jié)合實(shí)例形式分析了Python使用sklearn庫實(shí)現(xiàn)的KNN、SVM、LR、決策樹、隨機(jī)森林等算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07Python實(shí)現(xiàn)匯率轉(zhuǎn)換操作
這篇文章主要介紹了Python實(shí)現(xiàn)匯率轉(zhuǎn)換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05