检查两个文件夹的差异

2024-04-28 15:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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,如何正确地检查它们


Tags: and文件文件夹truereturnifcheckerror
1条回答
网友
1楼 · 发布于 2024-04-28 15:37:01

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

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'单向-vv-p组合日志文件.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的详细信息 一方面。带有“=”的文件在本地和远程目标上是相同的,因此我们希望将它们从源文件夹移动到上载的文件夹

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

仅供参考:

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

我当然知道这段代码可以更简单、更完善

相关问题 更多 >