python办公自动化_0524_02文件夹数据归类

桌面的数据整理 基于文件类型进行归类
office3_02.py

from os import walk, mkdir, remove
import os
from shutil import copy, move

# 桌面的数据整理  基于名字

def copyFile(path, newpath):
    for filepath, folders, files in walk(path):
        for file in files:
            if 'xlsx' in file:
                if os.path.exists(newpath+'\\xlsx'):
                    print(os.path.join(filepath), file)
                    copy(os.path.join(filepath,file), os.path.join(newpath+'\\xlsx',file))
                else:
                    print('xlsx folder is not exitsed and create folder')
                    os.mkdir(newpath+'\\xlsx')
                    copy(os.path.join(filepath, file), os.path.join(newpath + '\\xlsx', file))
            if 'pdf' in file:
                if os.path.exists(newpath + '\\pdf'):
                    print(os.path.join(filepath), file)
                    copy(os.path.join(filepath, file), os.path.join(newpath + '\\pdf', file))
                else:
                    print('pdf folder is not exitsed and create folder')
                    os.mkdir(newpath + '\\pdf')
                    copy(os.path.join(filepath, file), os.path.join(newpath + '\\pdf', file))
            if 'docx' in file:
                if os.path.exists(newpath+'\\docx'):
                    print(os.path.join(filepath), file)
                    copy(os.path.join(filepath,file), os.path.join(newpath+'\\docx',file))
                else:
                    print('docx folder is not exitsed and create folder')
                    os.mkdir(newpath+'\\docx')
                    copy(os.path.join(filepath, file), os.path.join(newpath + '\\docx', file))



if __name__=='__main__':
    path=r'C:\Users\fengz\Downloads'
    newpath=r'C:\Users\fengz\Desktop\5.24_copy'
    copyFile(path,newpath)


以上代码已做成函数,只需要输入pathnewpath 即可,可以用move 做剪切操作!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值