1

我正在尝试编写一个用于使用 rclone 上传到 gdrive 的自动脚本。我不会只在这个检查语句中查看所有代码,rclone 命令检查本地文件夹和挂载文件夹中的文件,如下所示: rclone check "local folder" "mounted folder" --ignore existing --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?

4

1 回答 1

0

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

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 check 'Local' 'gdrive' --one-way -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 的更多信息,请查看有关 rclone 的帮助 。带有“=”的文件在本地和远程目标上是相同的,因此我们希望将它们从源文件夹移动到上传的文件夹。

脚本再次运行,如果读取功能无法读取任何内容,则所有文件都在线,上传功能不需要再次运行。但是由于有未上传的文件和损坏的文件(如果在上传时连接丢失,可能会发生这种情况),脚本将运行上传函数或由带有变量“Error_upload”的 if 函数触发的任何其他函数

仅供参考:

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

我当然知道这段代码可以更简单和改进。

于 2020-10-14T13:47:53.247 回答