python 文件移动

我也搞不懂为啥移动不了 不能顺序排列

# 计划: 写一个文件排序 软件
# 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))
# 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")
View Code

 

posted @ 2021-12-13 14:34  inks  阅读(34)  评论(0)    收藏  举报