Python项目实战二:文件归类


    import os
    import shutil
    path = './'
    files = os.listdir(path)
    print(files)
    
    for f in files:
       folder_name = '.' + f.split('.')[-1]  # f是一个字符串,用'.'进行分割,['121212','zip'],加了 -1 则会取到zip
       if not os.path.exists(folder_name): #如果文件夹不存在则创建文件夹
           os.makedirs(folder_name)
           shutil.move(f,folder_name)#移动函数,将f移动到folder_name中去
       else:
           shutil.move(f,folder_name)

猜你喜欢

转载自blog.csdn.net/cxd15194119481/article/details/84930560