python 檔案移動
阿新 • • 發佈:2021-12-13
我也搞不懂為啥移動不了 不能順序排列
# 計劃: 寫一個檔案排序 軟體 # import shutil import os # 建立50個檔案 在建立資料夾 # for index in range(50): # if 9>=index>=0: # index = "0"+str(index) # else: # pass # with open("file_{}.txt".format(index),"w",encoding="utf-8") as f: # f.write("I am Hacker {}".format(index))View Code# os.mkdir("file") # 每當移動一個檔案 移動另一個檔案 import shutil try: a = os.listdir() b = [] c = [] for item in a: if os.path.isfile(item) and item.find("file") == 0: b.append(item) pass elif os.path.isfile(item) and item.find("music") == 0: #這裡可以寫mp3 c.append(item)except Exception as a: print(a) pass finally: n = 0 max = len(b) for index in c: # 移動一個c 在移動一個b shutil.move(index,"file") print('我移動了{}'.format(index)) if n>50: print("沒有那麼需要插入的檔案") break shutil.move(b[n], "file")print('我移動了{}'.format(b[n])) n +=1 print(b) print(c) # shutil.rmtree("file")