我正在尝试编写一个自动脚本,用于使用rclone上传到gdrive。我不会只在这个check语句中查看所有代码,rclone命令检查本地文件夹和挂载文件夹中的文件,如下所示:rclone check";本地文件夹"安装文件夹"--忽略现有--仅单向它在终端中返回一些无法存储在文本文件中的数据,或者我现在不知道如何存储。
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 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检查帮助的更多信息在rclone上。带有"="在本地和远程目的地上是相同的,所以我们想将它们从源文件夹移到上传的文件夹。
脚本再次运行,如果读取功能无法读取任何内容,则所有文件都处于联机状态,上载功能无需再次运行。但由于存在未上传的文件和损坏的文件(如果上传时连接丢失,则可能发生这种情况(,脚本将运行上传函数或由变量为"0"的if函数触发的任何其他函数;错误_上传">
仅供参考:
if Error_upload == True:
print("All files are on the cloud")
else:
upload() #your upload function
upload_check()
我当然知道这个代码可以更简单和改进。