Created
November 29, 2023 22:42
-
-
Save Edwardtonnn/8b2752153da55a50d6a177e8743f36f4 to your computer and use it in GitHub Desktop.
Renames all folders in numberic order 01 02 03...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
def rename_directories_to_sequential_numbers(path): | |
if not os.path.isdir(path): | |
print(f"The provided path '{path}' is not a valid directory.") | |
return | |
dirs = [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))] | |
dirs.sort() | |
# Check if number of folders exceed 99 | |
if len(dirs) > 99: | |
print("The script supports up to 99 directories only.") | |
return | |
for i, dir_name in enumerate(dirs, start=1): | |
new_dir_name = "{:02}".format(i) | |
os.rename(os.path.join(path, dir_name), os.path.join(path, new_dir_name)) | |
print(f"Renamed {len(dirs)} directories in '{path}'.") | |
if __name__ == "__main__": | |
dir_path = './' # Default path is the current directory | |
if len(sys.argv) > 1: | |
dir_path = sys.argv[1] | |
rename_directories_to_sequential_numbers(dir_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment