吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1628|回复: 23
收起左侧

[Python 原创] 同步文件夹

  [复制链接]
梦汐 发表于 2024-4-30 02:42
本帖最后由 梦汐 于 2024-4-30 02:43 编辑

说明:网盘要么有同步文件夹数量限制,要么不过滤就一股脑全上传整个文件夹,于是花了一两个小时写了个脚本优化一下,缺点是额外占用存储空间,原理就是py先检测差异,然后同步到本地文件夹,再通过网盘同步本地的同步文件夹

下面是我自用的JSON,功能就是从文档里挑选出list这几个子文件夹,然后同步文件到我指定的target目录,同步完再到网盘里添加D:\\Yunchu为同步目录就可以了,如果要同步source和target的文件需要重新运行脚本

作用大概就是可以无限添加同步文件夹,且可以只保留需要的子文件夹

json
{
    "Yunchu": "1.0",
    "enumeration": [
        {
            "source": {
                "mode": "contains",
                "path": "C:\\Users\\25678\\Documents",
                "list": [
                    "GitHub",
                    "My Games",
                    "图片"
                ]
            },
            "target": "D:\\Yunchu\\Documents"
        }
    ]
}


code
import filecmp
import os
import shutil
import json

def GetFileDiff(source_dir, target_dir, inherit=None):
    if inherit == None:
        this = []
    else:
        this = inherit

    dir_cmp = filecmp.dircmp(source_dir, target_dir)

    for path_name in dir_cmp.left_only:
        filepath = os.path.join(source_dir, path_name)
        filetype = "dir" if os.path.isdir(filepath) else "file"

        if filetype == "dir":  # 取出目录下的子文件
            for root, dir, files in os.walk(filepath):
                for file in files:  # 全文件夹进行复刻
                    source = os.path.join(root, file)
                    target = target_dir + source[len(source_dir):]
                    target = os.path.abspath(target)
                    bag = {
                        "source": source,
                        "target": target
                    }
                    this.append(bag)
        else:  # 非子目录新增文件
            source = os.path.join(source_dir, filepath)
            target = target_dir + source[len(source_dir):]
            target = os.path.abspath(target)
            bag = {
                "source": source,
                "target": target
            }
            this.append(bag)

    for path_name in dir_cmp.diff_files:  # 目标或源目录文件发生变化时更新目标目录文件
        this.append(os.path.join(source_dir, path_name))  # 文件压缩后会与源文件不同

    for path_name in dir_cmp.right_only:
        # 将目标文件夹中新增的文件移动到变更文件夹中或删除
        os.remove(os.path.abspath(os.path.join(target_dir, path_name)))

    for common_dir in dir_cmp.common_dirs:
        new_source = os.path.join(source_dir, common_dir)
        new_target = os.path.join(target_dir, common_dir)
        GetFileDiff(new_source, new_target, this)

    return this

config = None
with open("config.json", encoding="utf-8") as file:
    config = json.loads(file.read())

print(config)

def transfer(source, target):
    os.makedirs(source, exist_ok=True)
    os.makedirs(target, exist_ok=True)

    diffs = GetFileDiff(source,target)

    for diff in diffs:
        os.makedirs(os.path.dirname(diff['target']), exist_ok=True)
        shutil.copy(diff['source'], diff['target'])

for i, obj in enumerate(config['enumeration']):
    obj_type = type(obj['source'])
    if obj_type == dict:
        path = obj['source']['path']
        items = obj['source']['list']

        for item in items:
            source = os.path.join(path, item)
            target = os.path.join(obj['target'], item)

            transfer(source, target)
    else:
        source = obj['source']
        target = obj['target']
        transfer(source, target)
    print(i, obj)

免费评分

参与人数 9吾爱币 +15 热心值 +9 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
BigUncle52 + 1 + 1 热心回复!
blindcat + 1 + 1 谢谢@Thanks!
杨辣子 + 1 + 1 热心回复!
似水流年2015 + 1 + 1 坚果云,微力同步,都有增量同步功能吧
wujie9909 + 1 + 1 热心回复!
catti518 + 1 + 1 我很赞同!
qiaoyong + 1 + 1 热心回复!
为之奈何? + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

tinglie 发表于 2024-4-30 10:22
这是一个思路,目前市面上有很多的同步空间替代品,而且还是开源的。我这里用"FreeFileSync"
bnb 发表于 2024-4-30 03:01
myindenture 发表于 2024-4-30 06:53
1e3e 发表于 2024-4-30 08:37
谢谢楼主无私的分享
psporz 发表于 2024-4-30 08:38
谢谢楼主无私的分享
lzspain 发表于 2024-4-30 08:41
百度网盘的同步就会检测差异
yp5768 发表于 2024-4-30 08:56

谢谢楼主无私的分享
lg2023 发表于 2024-4-30 09:04
为什么上传总是比下载要快
cn63655 发表于 2024-4-30 09:35
学习了 谢谢楼主无私的分享
chr1sj0ne 发表于 2024-4-30 09:44
学习了 谢谢无私分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-17 07:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表