自动化整理文件夹

自动化整理文件夹,第1张

🍓🍉🍓🍉在现实中整理繁琐的文件夹相当麻烦,基于在整理过程中具有重复性 *** 作,可以通过python的自动化办公模块,os库和shutil库来实现自动化整理文件夹的目的。🍓🍉🍓🍉

话不多说直接上源码:

import os
import shutil
a = 1
dict1={"图片":"png","视频":"mp4","文档":"xlsx","pptx":"pptx","ppt":"ppt"}
while a == 1:
    print(dict1.keys())
    setdir = input("请选择您要创建的文件夹:")
    way = input("请输入您要创建文件夹的绝对路径")
    if setdir in dict1.keys():
        if os.path.exists(way):
            dirpath = os.path.join(way, setdir)
            if not os.path.exists(dirpath):  # 判断文件夹是否存在
                os.mkdir(dirpath)  # 创建文件夹
                print("新的文件夹已经创建成功!")
                file_list = os.listdir(way)  # 返回所有文件名称、
                want_typefile = dict1[setdir]
                for filename in file_list:  # i不能有_
                    type_file = filename.split('.')[-1]  # 获得文件类型
                    if want_typefile == type_file:       # 如果输入的文件类型与文件夹里面的文件格式相符
                        old_path = os.path.join(way, filename)
                        new_path = os.path.join(way,dirpath, filename)
                        shutil.move(old_path, new_path)
                print(f"成功将{setdir}类型文件归类")
                decide = input("您是否还要继续归类?继续归类输入q/Q,任意键结束")
                if decide == "q" or decide == "Q":
                    a = 1
                else:
                    a=0
            else:
                print("您要创建的文件夹已经存在")
        else:
            print("您输入的绝对路径不存在")
    else:
        print("您输入的内容不在选择列表中")
注意:

☺☺在输入文件绝对路径时,需要注意,输入的不能是字符串形式,这是因为input()返回值会加上引号,输入字符串就相当于在绝对路径外有双层引号,会导致os.path.exists()找不到文件。✔✔

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/917650.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存