feat: add support for converting folder names during Chinese character conversion
This commit is contained in:
parent
5a1f49d258
commit
25ccd53378
21
zhconv.py
21
zhconv.py
|
|
@ -28,12 +28,16 @@ def main():
|
||||||
|
|
||||||
cc = OpenCC('t2s')
|
cc = OpenCC('t2s')
|
||||||
|
|
||||||
|
original_cwd = os.getcwd()
|
||||||
|
is_folder = False
|
||||||
|
|
||||||
if os.path.isfile(input_arg):
|
if os.path.isfile(input_arg):
|
||||||
target_dir = os.path.dirname(input_arg)
|
target_dir = os.path.dirname(input_arg)
|
||||||
target_files = [os.path.basename(input_arg)]
|
target_files = [os.path.basename(input_arg)]
|
||||||
elif os.path.isdir(input_arg):
|
elif os.path.isdir(input_arg):
|
||||||
target_dir = input_arg
|
target_dir = input_arg
|
||||||
target_files = None
|
target_files = None
|
||||||
|
is_folder = True
|
||||||
else:
|
else:
|
||||||
print(f"Error: Path '{input_arg}' does not exist.")
|
print(f"Error: Path '{input_arg}' does not exist.")
|
||||||
return
|
return
|
||||||
|
|
@ -58,5 +62,22 @@ def main():
|
||||||
|
|
||||||
print(f"\nConverted {converted_count} file(s).")
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue