前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于拷贝QQ、微信、企业微信等软件聊天过程中保存的文件

关于拷贝QQ、微信、企业微信等软件聊天过程中保存的文件

作者头像
爱喝水的木子
发布2022-09-16 10:53:15
1.8K0
发布2022-09-16 10:53:15
举报
文章被收录于专栏:爱喝水的木子爱喝水的木子

为什么

关于拷贝QQ、微信、企业微信等软件聊天过程中保存的文件,下午的时候整理资料,发现了去年的文件,直接开搞,把数据给整理处理

逻辑

递归遍历文件夹中的文件,碰到符合条件的后缀文件进行拷贝,如果有重命名的进行添加一个uuid

常见文件格式

需要添加什么可以可以自行进行添加

代码

代码语言:javascript
复制
# coding=utf-8
# @autor 爱喝水的木子
# @Time : 2022/7/22
# @FileName : 资源汇总.py
import os
import shutil
import uuid

# 常见文件格式
file_suffix = ['txt', 'wav', 'mp3', 'mp4', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'zip', 'rar', '7z', 'gz',
               'json', 'xml', 'html', 'htm', 'css', 'js', 'java', 'c', 'cpp', 'h', 'hpp', 'py', 'java', 'c', 'cpp', 'h',
               ]


# 命名重复的话添加uuid
def rename_file(path):
    if os.path.exists(path):
        uuid_str = str(uuid.uuid1().hex)
        path = path.replace(os.path.splitext(path)[1], "_{}".format(uuid_str) + os.path.splitext(path)[1])
        return path
    else:
        return path


def yid_data_to_move(src):
    dst = os.path.join(base_dir, os.path.basename(src))
    # 校验是否存在
    while True:
        if os.path.exists(dst):
            dst = rename_file(dst)
        else:
            break
    try:
        shutil.copyfile(src, dst)
    except Exception as e:
        print("move failed:{},error source:{}".format(src, str(e)))


# 递归查找符合常见的文件格式的文件
def find_file(path):
    for file in os.listdir(path):
        temp_file = os.path.join(path, file)
        if os.path.isdir(temp_file):
            find_file(temp_file)
            print("dir:", temp_file)
        else:
            print("os.path.splitext(temp_file)[1][1:]", os.path.splitext(temp_file)[1][1:])
            if os.path.splitext(temp_file)[1][1:] in file_suffix:
                print("file:{}".format(temp_file))
                yid_data_to_move(temp_file)


if __name__ == '__main__':
    # 基础路径
    base_source = r""
    # 保存的路径
    base_dir = r""
    find_file(base_source)
    print("end of find")
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-07-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 为什么
  • 逻辑
  • 常见文件格式
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档