Aug-20-2020, 08:12 PM
This is my first code. I basically tried to write a programm which is able to change the names of multiple files or numbering them.
If you have some adives to write it more efficiently or something else please share it with me. Sorry for spelling mistakes ;D
If you have some adives to write it more efficiently or something else please share it with me. Sorry for spelling mistakes ;D
##################################################################### # shall be able of: # # changing names of all files in one directory into the same name # # give each file a number in ascending order # # delete a part of the existing name # ##################################################################### ################################################################################ ## Importing Modules ########################################################## ################################################################################ import os split = "********************************************************************************" ################################################################################ ## First Part ################################################################ ################################################################################ #chosing the path while True: try: path = input("Please enter the path of your destination folder: ") os.chdir(path) break except FileNotFoundError: print("Path does not exist!\n\n") except OSError: print('This is not a valid path\n\n') print("\n\nThese are the names of your folders in the choosen directory:\n") #displaying the selected files for file in os.listdir('.'): print(file) #displaying the options first_option = "To create completely new names enter 1" second_option = "To delete a part of the existing name enter 2" third_option = "To number all the files in an ascending order enter 3" fourth_option = "To exit just click on the X in the upper right corner" print(f'\n\n{first_option}\n{second_option}\n{third_option}\n{fourth_option}\n\n') #choosing the options while True: try: option=int(input("\nPlease enter on of the given numbers(1, 2, 3) to choose an option: ")) if option == 1: break elif option == 2: break elif option == 3: break else: continue except ValueError: print("Not a valid Number!") #creating backup list backup = [] for file in os.listdir('.'): backup = backup+[file] ################################################################################ ## Second Part ############################################################## ################################################################################ ####################################################################################### ######## All Options ############################################################# ####################################################################################### #rename all files and additionally number them if wanted if option == 1: new_name = input("\nPlease insert the new name for your files: ") while True: choose = input("Do you also want to number your files additionally? (Y/N) : ").lower().strip() try: #.lower changes all capitals to normal letters if choose[0] == 'n': #.strip ignores all whitespace for file in os.listdir('.'): try: pre, ext = file.split('.') final_name = f'{new_name}.{ext}' os.rename(file, final_name) except ValueError: try: os.rename(file, new_name) except PermissionError: print("Permission denied, Windows doesn't allow changing names in this directory!") except FileExistsError: print(file, "can't be changed, name is already assigned!") except FileExistsError: print(file, "can't be changed, name is already assigned!") except PermissionError: print("Permisiion denied, Windows doesn't allow changing names in this directory!") break elif choose[0] == 'y': number = 1 for file in os.listdir('.'): try: pre, ext = file.split('.') final_name = f'{number} {new_name}.{ext}' os.rename(file, final_name) int(number) number += 1 except ValueError: final_name = f'{number} {new_name}' os.rename(file, final_name) number += 1 break else: print("Not a valid Input!") continue except IndexError: print("Press either Y or N to continue.\n") ####################################################################################################################################### #deleting a part of the existing name and additionally add a number if wanted elif option == 2: while True: choose = input("\nDo you also want to number your files additionally? (Y/N) : ").lower().strip() try: if choose[0] == 'n': print("Please define the part which you want to delete by entering first the start and then the end symbol.\n") print(split) print("\nFor example:\n\nHallo(123LOL321).txt") print("Hallo(12hdk21)Boy") print("Hallo(12dhds21)Girl") print("Hallo(12asd21)Beauty") print("\nInto:\nHallo(123LOL321).txt\nHallo(12hdk21)Boy\nHallo(12dhds21)Girl\nHallo(12asd21)Beauty\n") print("\nTo delete everything between the brackets enter: 1 and 1 .") print("To delete everything between the brakets including the brackets enter: ( and ) .") print("\nNotice the first symbol you enter is the symbol from which every other letter\nwill be deleted up to the second symbol you've entered!\n") print(split) print("** WARNING! If there is a dot inside the name, it won't work **") print("** In this case please restart the programm and use the first option instead.**") print(split) print("\n\n\nThese are the names of your files:\n\n") for file in os.listdir('.'): try: Q, W = file.split('.') print(Q) except ValueError: print(file) print("\n") A = input("Please enter the first symbol: ") B = input("Please enter the second symbol: ") for file in os.listdir('.'): try: pre, ext = file.split('.') front, back = pre.split(A) mid, backnd = back.split(B) final_name = f'{front}{backnd}.{ext}' os.rename(file, final_name) except ValueError: try: front, back = file.split(A) mid, backnd = back.split(B) final_name = f'{front}{backnd}' os.rename(file, final_name) except ValueError: print(file, "can't be changed because the entered splitpoint isn't available!") break elif choose[0] == 'y': print("Please define the part which you want to delete by entering first the start and then the end symbol.\n") print(split) print("\nFor example:\n\nHallo(123LOL321).txt") print("Hallo(12hdk21)Boy") print("Hallo(12dhds21)Girl") print("Hallo(12asd21)Beauty") print("\nInto:\nHallo(123LOL321).txt\nHallo(12hdk21)Boy\nHallo(12dhds21)Girl\nHallo(12asd21)Beauty\n") print("\nTo delete everything between the brackets enter: 1 and 1 .") print("To delete everything between the brakets including the brackets enter: ( and ) .") print("\nNotice the first symbol you enter is the symbol from which every other letter\nwill be deleted up to the second symbol you've entered!\n") print(split) print("** WARNING! If there is a dot inside the name, it won't work **") print("** In this case please restart the programm and use the first option instead.**") print(split) print("\n\n\nThese are the names of your files:\n\n") for file in os.listdir('.'): try: Q, W = file.split('.') print(Q) except ValueError: print(file) print("\n") A = input("Please enter the first symbol: ") B = input("Please enter the second symbol: ") number = 1 for file in os.listdir('.'): try: pre, ext = file.split('.') front, back = pre.split(A) mid, backnd = back.split(B) final_name = f'{number} {front}{backnd}.{ext}' os.rename(file, final_name) number += 1 except ValueError: try: front, back = file.split(A) mid, backnd = back.split(B) final_name = f'{number} {front}{backnd}' os.rename(file, final_name) number += 1 except ValueError: print(file, "can't be changed because the entered splitpoint isn't available!") break else: print("Not a valid Input!") except IndexError: print("Press either Y or N to continue.\n") ################################################################################################################################################ #numbering all the files elif option == 3: while True: choose = input("Do you want to delete the current name and replace it by the numbers? (Y/N): ").lower().strip() try: if choose[0] == 'n': number = 1 try: for file in os.listdir('.'): final_name = f'{number} {file}' os.rename(file, final_name) number += 1 except PermissionError: print("Windows doesn't allow any changes here!") break elif choose[0] == 'y': number = 1 try: for file in os.listdir('.'): pre, ext = file.split('.') final_name = f'{number} .{ext}' number += 1 except ValueError: try: for file in os.listdir('.'): final_name = f'{number}' os.rename(file, final_name) number += 1 except PermissionError: print("Windows doesn't allow any changes here!") break except IndexError: print("Press either Y or N to continue.\n") print("\n\n\n\n") print(split) print("\n\nNames have been changed to:\n") for file in os.listdir('.'): print(file) ############################################################################## # BACKUP ################################################################# ############################################################################## while True: choose_backup = input("\n\nDo you want to undo your action? Last chance (Y/N): ").lower().strip() try: if choose_backup[0] == 'n': break elif choose_backup[0] == 'y': N = 0 for file in os.listdir('.'): try: os.rename(file, backup[N]) N += 1 except IndexError: break break except IndexError: print("Press either Y or N to continue.\n") print("\nDone") input("\n\nPress any key to exit")