# -*- coding: cp936 -*- import os ,string ,urllib ,operator # 檔案替換目錄路徑 sdir='/usr/local/mldonkey/incoming/' # 數字標記 sNum='0123456789' # 遍曆目錄做檔案名稱轉換 def convert(): filenames=os.listdir(sdir) for filename in filenames : if filename != convertName(filename) : print filename + ' >>> ' + convertName(filename) os.rename(sdir + filename ,sdir + convertName(filename)) # 轉換一個檔案名稱 def convertName(s) : location = 0 ret = "" while True : if location + 8 <= len(s) : subStr = s[location:location + 8] if check(subStr) : ret += "%" + hex((int)(subStr[1:4]))[2:4] + "%" + hex((int)(subStr[5:8]))[2:4] location = location + 8 else : ret += s[location :location + 1] location = location + 1 else : ret += s[location:] break return urllib.unquote(ret) # 檢查一個字串是否需要做轉換 def check(s): if len(s) != 8 : return False if s[0] != '_' or s[4] != '_' : return False if (s[1] in sNum) and (s[2] in sNum) and (s[3] in sNum) and (s[5] in sNum) and (s[6] in sNum) and (s[7] in sNum) : return True return False # 執行檔案名稱轉換 convert() |