diff --git a/zhconv.py b/zhconv.py index c494249..55ee892 100644 --- a/zhconv.py +++ b/zhconv.py @@ -28,12 +28,16 @@ def main(): cc = OpenCC('t2s') + original_cwd = os.getcwd() + is_folder = False + if os.path.isfile(input_arg): target_dir = os.path.dirname(input_arg) target_files = [os.path.basename(input_arg)] elif os.path.isdir(input_arg): target_dir = input_arg target_files = None + is_folder = True else: print(f"Error: Path '{input_arg}' does not exist.") return @@ -58,5 +62,22 @@ def main(): print(f"\nConverted {converted_count} file(s).") + os.chdir(original_cwd) + + if is_folder: + folder_name = os.path.basename(target_dir) + new_folder_name = convert_filename(folder_name, cc) + if new_folder_name: + parent_dir = os.path.dirname(target_dir) + new_target_dir = os.path.join(parent_dir, new_folder_name) + if os.path.exists(new_target_dir): + print(f"Skipped folder (already exists): {new_folder_name}") + else: + try: + os.rename(target_dir, new_target_dir) + print(f"Converted folder: {folder_name} -> {new_folder_name}") + except Exception as e: + print(f"Warning: Could not rename folder '{folder_name}'. Ensure it is not in use. Error: {e}") + if __name__ == "__main__": main()