From 7cf979620a4b75e0600d29cdada5c70561da07e3 Mon Sep 17 00:00:00 2001 From: yuxing Date: Fri, 26 Sep 2025 15:54:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0UTF8=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E4=BB=A5=E6=AD=A3=E7=A1=AE=E6=98=BE=E7=A4=BA=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Robocopy_GUI/robocopy_mirror_gui.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Robocopy_GUI/robocopy_mirror_gui.py b/Robocopy_GUI/robocopy_mirror_gui.py index 8117244..f8a2548 100644 --- a/Robocopy_GUI/robocopy_mirror_gui.py +++ b/Robocopy_GUI/robocopy_mirror_gui.py @@ -5,6 +5,7 @@ import subprocess import threading import platform import queue # For thread-safe communication +import locale # For getting system's default encoding # --- Configuration --- # Robocopy Flags for Mirroring (/MIR) @@ -142,6 +143,8 @@ class RobocopyMirrorApp: # Renamed class for clarity def run_robocopy_process(self, source, target): """The actual function running in the thread.""" try: + # Determine the console's output encoding, crucial for non-English filenames + output_encoding = locale.getpreferredencoding(False) # Use the MIRROR_ROBOCOPY_FLAGS command = ["robocopy", source, target] + MIRROR_ROBOCOPY_FLAGS @@ -149,18 +152,17 @@ class RobocopyMirrorApp: # Renamed class for clarity command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, # Redirect stderr to stdout - text=True, - encoding='utf-8', - errors='replace', + # text=True and encoding are removed to read raw bytes bufsize=1, creationflags=subprocess.CREATE_NO_WINDOW # Prevent cmd window popup ) - # Read output line by line - for line in iter(self.process.stdout.readline, ''): + # Read output line by line as bytes and decode + for line in iter(self.process.stdout.readline, b''): if not line: # Check if process has exited break - self.output_queue.put(line) + # Decode using the system's preferred encoding and put into the queue + self.output_queue.put(line.decode(output_encoding, errors='replace')) if self.process.poll() is not None and not line: # Double check if process ended and no more output break