refactor: simplify file path handling by changing directory to target folder before processing
This commit is contained in:
parent
7ff2e94059
commit
5a1f49d258
16
zhconv.py
16
zhconv.py
|
|
@ -30,7 +30,7 @@ def main():
|
|||
|
||||
if os.path.isfile(input_arg):
|
||||
target_dir = os.path.dirname(input_arg)
|
||||
target_files = [input_arg]
|
||||
target_files = [os.path.basename(input_arg)]
|
||||
elif os.path.isdir(input_arg):
|
||||
target_dir = input_arg
|
||||
target_files = None
|
||||
|
|
@ -38,21 +38,21 @@ def main():
|
|||
print(f"Error: Path '{input_arg}' does not exist.")
|
||||
return
|
||||
|
||||
os.chdir(target_dir)
|
||||
|
||||
if target_files is None:
|
||||
target_files = glob.glob(os.path.join(target_dir, '*'))
|
||||
target_files = glob.glob('*')
|
||||
|
||||
converted_count = 0
|
||||
for file_path in target_files:
|
||||
if not os.path.isfile(file_path):
|
||||
for filename in target_files:
|
||||
if not os.path.isfile(filename):
|
||||
continue
|
||||
filename = os.path.basename(file_path)
|
||||
new_filename = convert_filename(filename, cc)
|
||||
if new_filename:
|
||||
new_path = os.path.join(target_dir, new_filename)
|
||||
if os.path.exists(new_path):
|
||||
if os.path.exists(new_filename):
|
||||
print(f"Skipped (already exists): {new_filename}")
|
||||
else:
|
||||
os.rename(file_path, new_path)
|
||||
os.rename(filename, new_filename)
|
||||
print(f"Converted: {filename} -> {new_filename}")
|
||||
converted_count += 1
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue