Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- def main():
- # Get user input for the file extension, prefix, and suffix
- file_ext = input("Enter the file extension type: ").strip().lower()
- prefix = input("Enter the prefix string: ")
- suffix = input("Enter the suffix string: ")
- # Ensure the file extension does not start with a dot
- if not file_ext.startswith('.'):
- file_ext = '.' + file_ext
- # Get the current directory
- current_directory = os.path.dirname(os.path.realpath(__file__))
- # Loop through every file in the directory
- edited_files = []
- for filename in os.listdir(current_directory):
- # If the file has the desired extension
- if filename.lower().endswith(file_ext):
- # Rename the file with the prefix and suffix
- new_name = prefix + filename.rsplit('.', 1)[0] + suffix + file_ext
- os.rename(os.path.join(current_directory, filename), os.path.join(current_directory, new_name))
- edited_files.append((filename, new_name))
- # Display the edited files
- if edited_files:
- print("\nEdited files:")
- for old_name, new_name in edited_files:
- print(f'{old_name} -> {new_name}')
- print("\nFinished renaming files!")
- else:
- print("No files with the specified extension found.")
- # Wait for user input to close
- input("\nPress Enter to exit...")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment