Z230
This commit is contained in:
46
50 Různé testy/10 RenameFoldersTriangles.py
Normal file
46
50 Různé testy/10 RenameFoldersTriangles.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Define the target directory
|
||||
target_path = Path(r"U:\Dropbox\Ordinace\Dokumentace_ke_zpracování\MP")
|
||||
|
||||
|
||||
def rename_folders():
|
||||
# Ensure the path exists
|
||||
if not target_path.exists():
|
||||
print(f"Error: The path {target_path} does not exist.")
|
||||
return
|
||||
|
||||
# Iterate through items in the directory
|
||||
for folder in target_path.iterdir():
|
||||
# Only process directories
|
||||
if folder.is_dir():
|
||||
original_name = folder.name
|
||||
|
||||
# Check if name starts with the triangle
|
||||
if original_name.startswith("▲"):
|
||||
# 1. Remove the triangle from the start
|
||||
name_without_tri = original_name[1:]
|
||||
|
||||
# 2. Prepare the name to be at least 10 chars long
|
||||
# (so the triangle can sit at index 10 / position 11)
|
||||
clean_name = name_without_tri.ljust(10)
|
||||
|
||||
# 3. Construct new name: first 10 chars + triangle + the rest
|
||||
new_name = clean_name[:10] + "▲" + clean_name[10:]
|
||||
|
||||
# Remove trailing spaces if the original name was short
|
||||
# but you don't want extra spaces at the very end
|
||||
new_name = new_name.rstrip()
|
||||
|
||||
new_folder_path = folder.parent / new_name
|
||||
|
||||
try:
|
||||
print(f"Renaming: '{original_name}' -> '{new_name}'")
|
||||
folder.rename(new_folder_path)
|
||||
except Exception as e:
|
||||
print(f"Could not rename {original_name}: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
rename_folders()
|
||||
Reference in New Issue
Block a user