【问题标题】:Is there a way to track robocopy's progress有没有办法跟踪 robocopy 的进度
【发布时间】:2018-12-21 13:34:09
【问题描述】:

我用 tkinter 制作了一个进度条。我试图让进度条跟随复制进度。有没有办法查看 robocopy 的进度并将其用于进度条?

我知道通过计算源目录的大小然后检查目标目录的大小有某种解决方案。它确实有效,但我无法检查 robocopy 是否跳过了任何内容,然后进度条将永远显示。

我将此代码用于 robocopy:

subprocess.call(["robocopy", os.path.dirname(file_path), new_destination, filename[0], "/V", "/ETA", "/W:3", "/R:3",
                          "/TEE", "/LOG+:" +
                          new_destination + "copy.log"])

如果可能的话,我想用 robocopy 输出的进度更新进度条,如果不可能,我想用一种方法来检查 robocopy 是否完成。

提前致谢!

【问题讨论】:

  • 恐怕您需要在复制大量文件并监控进度(基于复制文件的数量)和复制一个非常大的文件(基于数量)之间进行拆分已复制的(千/兆)字节。
  • 抱歉回复晚了。我找到了一种跟踪进度的方法。也许这不是最好的方法,但对我来说已经足够了。请看我的回答我是怎么做到的。感谢您花时间查看并回答我的问题。

标签: python tkinter subprocess robocopy


【解决方案1】:

首先,当我按下一个按钮并选择了我想要复制的文件/目录时,我会检查它们组合在一起的大小。我保存它并将进度条的最大值设置为总大小。然后执行 start_calc 方法。

for item in self.file_and_directory_list:
            folder = ""
            my_file = ""
            if os.path.isdir(item):
                folder = item
            else:
                my_file = item

            if folder != "":
                print("folder")
                for (path, dirs, files) in os.walk(folder):
                    for file in files:
                        filename = os.path.join(path, file)
                        folder_size += os.path.getsize(filename)
            else:
                folder_size += os.path.getsize(item)

        if os.path.exists(new_destination):
            for (path, dirs, files) in os.walk(new_destination):
                for file in files:
                    filename = os.path.join(path, file)
                    folder_size += os.path.getsize(filename)

        processing_bar_copy_files["maximum"] = folder_size

        processing_bar_copy_files["value"] = 0

        threading.Thread(target=self.start_calc, args=(folder_size, new_destination)).start()   

我计算目标目录的大小并检查它是否比源目录更小的 start_calc 方法。如果它更小,则将进度条的值设置为目标目录的大小:

def start_calc(self, source_folder_size, new_destination):
    folder = new_destination
    dest_folder_size = 0

    try:
        for (path, dirs, files) in os.walk(folder):
            for file in files:
                filename = os.path.join(path, file)
                dest_folder_size += os.path.getsize(filename)
    except Exception as error:
        tkinter.messagebox.showerror(f"Er is iets fout gegaan:\n{error}")
    finally:
        if processing_bar_copy_files["value"] < processing_bar_copy_files["maximum"]:
            processing_bar_copy_files["value"] = dest_folder_size
            progress_percentage = (processing_bar_copy_files["value"] / processing_bar_copy_files["maximum"]) * 100
            progress_text_var.set(str("{:.1f}".format(progress_percentage) + "%"))
            self.size = dest_folder_size
            window.after(500, self.start_calc, source_folder_size, new_destination)

如果文件的复制完成,进度条被移除并停止,按钮被放回。

【讨论】:

    猜你喜欢
    • 2012-06-14
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    相关资源
    最近更新 更多