Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: extension_manager.py
- # Version: 1.0.1
- # Author: Jeoi Reqi
- """
- Description:
- - This script automates the renaming process for files within the current directory.
- - It allows users to easily append or replace filenames with a chosen file extension.
- - Users can select from various file extension options, including removing existing extensions or defining custom ones.
- - The tool streamlines file management by ensuring consistent and clear naming conventions.
- Requirements:
- - Python 3.x
- - os module
- Functions:
- - display_menu(): Displays a menu for the user to select a file extension.
- Extension Types and Descriptions:
- 1. .avif - AV1 Image File Format, a next-generation image format designed for efficiency.
- 2. .bmp - Bitmap Image File, a raster graphics image file format used to store bitmap digital images.
- 3. .eps - Encapsulated PostScript, a graphics file format used for vector images.
- 4. .gif - Graphics Interchange Format, a bitmap image format that supports animations and transparency.
- 5. .heic - High Efficiency Image File Format, a file format for photos and images with better compression.
- 6. .ico - Icon Image File, used for small graphics in the form of symbols, icons, etc.
- 7. .jpg - Joint Photographic Experts Group, a widely used image format known for its lossy compression.
- 8. .jpeg - JPEG File Interchange Format, similar to .jpg but used to describe the file format more explicitly.
- 9. .pdf - Portable Document Format, a file format used to present documents in a manner independent of software, hardware, and operating systems.
- 10. .png - Portable Network Graphics, a raster-graphics file format that supports lossless data compression.
- 11. .psd - Photoshop Document, a file format used to create and edit raster graphics in Adobe Photoshop.
- 12. .raw - Raw Image Data File, contains minimally processed data from a digital camera's image sensor.
- 13. .svg - Scalable Vector Graphics, an XML-based vector image format for two-dimensional graphics.
- 14. .tga - Truevision Targa Graphic, a raster graphics file format commonly used for storing high-quality images with full-color support and transparency.
- 15. .tiff - Tagged Image File Format, a flexible raster graphics file format that supports lossless compression.
- 16. .webp - WebP Image Format, a modern image format that provides superior lossless and lossy compression for images on the web.
- - main(): Main function to execute the renaming of files based on user input.
- Usage:
- - Run the script and follow the prompts to select a file extension for renaming files.
- Additional Notes:
- - Files with extensions .py and .txt are excluded from renaming.
- - Directories are excluded from renaming.
- - Choosing 'Remove Extensions' deletes current extensions from file names.
- - Selecting 'Custom Extension' permits entering a preferred extension.
- """
- import os
- # Generate an ascii header
- def header():
- print("""
- ____ ____ ____ ____ ____ ____ ____ ____ ____
- ||E ||||x ||||t ||||e ||||n ||||s ||||i ||||o ||||n ||
- ||__||||__||||__||||__||||__||||__||||__||||__||||__||
- |/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\|
- ____ ____ ____ ____ ____ ____ ____ ____ ____
- || ||||M ||||a ||||n ||||a ||||g ||||e ||||r |||| ||
- ||__||||__||||__||||__||||__||||__||||__||||__||||__||
- |/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\|
- """)
- # Function to display the menu and get user selection
- def display_menu():
- header()
- print("[Select An Extension To Convert All Files In The Current Directory]\n")
- print("\t 1. .avif\t\t 2. .bmp")
- print("\t 3. .eps\t\t 4. .gif")
- print("\t 5. .heic\t\t 6. .ico")
- print("\t 7. .jpg\t\t 8. .jpeg")
- print("\t 9. .pdf\t\t10. .png")
- print("\t11. .psd\t\t12. .raw")
- print("\t13. .svg\t\t14. .tga")
- print("\t15. .tiff\t\t16. .webp")
- print("\t17. [Remove Extensions]\t18. [Custom Extension]")
- print("\n\t[Or... Type '0' To Exit]")
- while True:
- choice = input("\nEnter the number corresponding to your choice: ")
- print("_" * 80)
- if choice == "1":
- return ".avif"
- elif choice == "2":
- return ".bmp"
- elif choice == "3":
- return ".eps"
- elif choice == "4":
- return ".gif"
- elif choice == "5":
- return ".heic"
- elif choice == "6":
- return ".ico"
- elif choice == "7":
- return ".jpg"
- elif choice == "8":
- return ".jpeg"
- elif choice == "9":
- return ".pdf"
- elif choice == "10":
- return ".png"
- elif choice == "11":
- return ".psd"
- elif choice == "12":
- return ".raw"
- elif choice == "13":
- return ".svg"
- elif choice == "14":
- return ".tga"
- elif choice == "15":
- return ".tiff"
- elif choice == "16":
- return ".webp"
- elif choice == "17":
- return "" # Empty string to remove extensions
- elif choice == "18":
- custom_extension = input("\nEnter the custom extension (without preceding dot): ")
- print("_" * 80)
- return f".{custom_extension}"
- elif choice == "0":
- print("\nExiting Program... GoodBye!")
- print("_" * 80)
- exit()
- else:
- print("Invalid choice. Please try again.")
- # Get the current working directory
- cwd = os.getcwd()
- # Get the desired extension from the user
- new_extension = display_menu()
- # Loop through all files in the directory
- for filename in os.listdir(cwd):
- # Get the full path of the file
- file_path = os.path.join(cwd, filename)
- # Check if the item is a file (not a directory), and is not a .py or .txt file
- if os.path.isfile(file_path) and not filename.endswith((".py", ".txt")):
- # Determine the new filename
- if new_extension == "":
- # Remove the extension
- new_filename = os.path.splitext(filename)[0]
- elif not os.path.splitext(filename)[1]:
- # Only add the new extension if there is no extension currently
- new_filename = filename + new_extension
- else:
- # Skip files that already have an extension if we are not removing extensions
- continue
- # Get the new file path
- new_file_path = os.path.join(cwd, new_filename)
- # Rename the file
- os.rename(file_path, new_file_path)
- print(f"Renamed {file_path} to {new_file_path}")
- print()
- print("_" * 80)
- print("\nExiting Program... GoodBye!")
- print("_" * 80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement