重新命名批處理python指令碼

來源:互聯網
上載者:User
將Copy of ********.bmp或者Copy of Copy of ********.bmp 此類檔案統一命名為********0.bmp 或者********00.bmp等格式,後面的0的個數代表********.bmp出現的次數+1。寫了個下面的小指令碼:

代碼如下:


import os
"""
這個程式是用來將檔案名稱改名,因為在檔案夾裡面有很多Copy of 重檔案名稱,因此去掉windows
重新命名風格而換用新的累加命名機制
作者:chenxofHit@gmail.com
時間:2011年4月13日
"""
def getFileNames(dirs, dict):
#dir為目錄名,lst列表
filenames=os.listdir(dirs)
for name in filenames:
key = filenames[filenames.index(name)]
sign = "Copy Of "
judge = key.find(sign)
if (judge != -1 ):
dict[key] = 1
else: #提取其中的真實檔案名稱
trueKey= key[-12:] #因為真實的檔案名稱都是在12位
if trueKey not in dict: #字典中不存在
dict[trueKey]=1
os.rename(dirs+str(os.sep)+name,dirs+str(os.sep)+trueKey)
else:
#split finames
newKey= str(trueKey[:-4])+str('0'*dict[trueKey])+ str(trueKey[-3:])
dict[trueKey] = dict[trueKey]+1
os.rename(dirs+os.sep+name,dirs+os.sep+newKey)

if '__name__=__main__':
dict={}
dirs = "C://temp"
getFileNames(dirs, dict)


用到了字典,用到了os模組,學到了一些東西,呵呵!
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.