Created
May 14, 2022 07:19
-
-
Save Alexandr6363/d8b6b985fdcdfa07a3fa0f8c00c6b36e to your computer and use it in GitHub Desktop.
A simple program in Python for renaming multiple files that checks and it does not overwrite old files that match to our pattern
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
from os import rename, path, listdir | |
try: | |
path_of_dir = input('Enter path of directory with your file:') | |
ext = input('Print file extension (unquoted) (".jpg" for example):') | |
list_of_filename = listdir(path_of_dir) | |
count = 1 | |
for file in list_of_filename: | |
full_old_name = path.join(path_of_dir, file) | |
new_name = "{:0>6}".format(count) + ext | |
while new_name in list_of_filename: | |
count += 1 | |
new_name = "{:0>6}".format(count) + ext | |
full_new_name = path.join(path_of_dir, new_name) | |
rename(full_old_name, full_new_name) | |
count += 1 | |
except FileNotFoundError: | |
print("Invalid path of directory. You must write valid path\ | |
\nfor example: 'F:/somedirectory' for windows or '/home/usr/somedirectory' for linux)") | |
except Exception as e: | |
print(f'Error {e}') | |
else: | |
print('Renaming completed successfully') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment