Python rclone检查两个文件夹的区别

问题描述

我正在尝试编写一个自动脚本,以使用rclone上传到gdrive。 我不会仅在此检查语句中浏览所有代码,rclone命令检查本地文件夹和已装载文件夹中的文件,如下所示: rclone检查“本地文件夹”“挂载的文件夹”-忽略现有的--onlyoneway 它会在终端中返回一些无法存储在文本文件中的数据,或者我现在不知道如何存储该数据。

def upload_check():
    print(" check if all files are uploaded ")
    global Error_upload
    if :#I stuck here,rclone check and return true or false if all files are uploaded by name and size
        Error_upload = True
        return Error_upload
        print("Not uploaded ")#---------------------------
    else:# all good
        Error_upload = False
        return Error_upload
        print("all files are online")#---------------------------

我的问题是,如果里面的所有文件文件大小都相同,并且返回布尔值True或False,如何正确检查两个目录?

解决方法

几天后,我想到了这个复杂的解决方案:

import shutil
import os
local = "Local/"
destination = "uploaded/"
checkfile = "logfile.txt"
def upload_check():
    print(" check if all files are uploaded ")
    global Error_upload 
    os.system("rclone check 'Local' 'gdrive' --one-way  -vv -P --combined logfile.txt")
    destination = "uploaded/"
    checkfile = "logfile.txt"
    search = "=" # move from the folder successfuly uplouded files

    list_of_files = []
    lines = []
    folders = []
    uniq_folder_list = []
    shutil_l = []
    shutil_f = []
    
    for line in open(checkfile,"r"):
        if search in line:
            list_of_files = line.split("/")[1]
            lines.append(list_of_files.rstrip())
            list_of_folders = line.split(" ")[1].split("/")[0]
            folders.append(list_of_folders.rstrip())
    [uniq_folder_list.append(n) for n in folders if n not in uniq_folder_list] 
    for new_folder in uniq_folder_list:
        if not os.path.exists(destination + new_folder):
            os.makedirs(destination + new_folder)
    for l,f in zip(lines,folders):
        l1 = (local + f + "/" + l)
        f1 = (destination + f)
        shutil_l.append(l1.rstrip())
        shutil_f.append(f1.rstrip())
    for src,dest in zip(shutil_l,shutil_f):
        shutil.move(src,dest)

    os.system("rclone check 'Local' 'gdrive' --one-way  -vv -P --combined logfile.txt")
    with open(checkfile,'r') as read_obj:
        one_char = read_obj.read(1)
        if not one_char:
            Error_upload = False
            return Error_upload
            print("all files are online")
        else:
            Error_upload = True
            return Error_upload
            print("Not uploaded ")

首先,我创建了一些文件,并将其中的一些上传到了驱动器中,这也是一个损坏的文件。胜过这份工作。 文件 logfile.txt 包含使用rclone生成的列表

rclone检查'Local''gdrive'-单向-vv -P --combined logfile.txt

此bash命令将生成一个日志文件:

+ 20_10_10/IMG_1301-00006.jpg
+ 20_10_10/IMG_1640-00007.jpg
+ 20_10_10/IMG_1640-00008.jpg
+ 20_10_10/IMG_1640-00009.jpg
+ 20_10_10/IMG_1640-00010.jpg
+ 20_10_10/IMG_1640-00011.jpg #missing on remote
* 20_10_10/IMG_1301-00004.jpg #corrupted file
= 20_10_10/IMG_1301-00005.jpg
= 20_10_10/IMG_1301-00003.jpg
= 20_10_10/IMG_1301-00001.jpg
= 20_10_09/IMG_2145-00028.jpg
= 20_10_10/IMG_1301-00002.jpg

有关rclone check help的更多信息 在rclone上。带有“ =”的文件在本地和远程目标上都是相同的,因此我们要将它们从源文件夹移动到上载文件夹。

该脚本将再次运行,并且如果读取功能无法读取任何内容,则所有文件均处于联机状态,并且无需再次运行上载功能。但是,由于存在未上传的文件和损坏的文件(如果在上传过程中失去连接,可能会发生),脚本将运行上传功能或if函数使用变量“ Error_upload”触发的其他功能

仅供参考:

if Error_upload == True:
   print("All files are on the cloud")
else:
   upload() #your upload function
   upload_check()
    

我当然知道这段代码可能会更简单和得到改进。