批量重新命名,檔案結構
阿新 • • 發佈:2018-12-15
import os f = open("./name_test.txt", 'w') # 先建立一個空的文字 f_jpg = open("./name_jpg.txt", 'w') f_txt = open("./name_txt.txt", 'w') path = "./jzchina4/" # 指定需要讀取檔案的目錄' files = os.listdir(path) # 採用listdir來讀取所有檔案 files.sort() # 排序 s = [] # 建立一個空列表 num = 1.0 #批量修改檔名 for file_ in files: str_num = "L"+str(int(num)).zfill(4) left,right = os.path.splitext(file_) new_name = str_num + right # new_name = "f" + file_.replace("f","") print(new_name) num = num + 0.5 os.rename(path + file_, path + new_name) print("檔名修改完畢!\n") files = os.listdir(path) files.sort() # 輸出至檔案 for file_ in files: # 迴圈讀取每個檔名 # print(path +file_) if not os.path.isdir(path + file_): # 判斷該檔案是否是一個資料夾 f_name = str(file_) # print(f_name) s.append(f_name) # 把當前檔名返加到列表裡 f.write(f_name + '\n') # 寫入之前的文字中 if str(file_).endswith('jpg'): f_jpg.write(str(file_) + "\n") elif str(file_).endswith('txt'): f_txt.write(str(file_) + "\n") print(s) # 看一下列表裡的內容