Advertisement
Python253

extension_manager

Jun 18th, 2024 (edited)
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.00 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: extension_manager.py
  4. # Version: 1.0.1
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script automates the renaming process for files within the current directory.
  10.    - It allows users to easily append or replace filenames with a chosen file extension.
  11.    - Users can select from various file extension options, including removing existing extensions or defining custom ones.
  12.    - The tool streamlines file management by ensuring consistent and clear naming conventions.
  13.  
  14. Requirements:
  15.    - Python 3.x
  16.    - os module
  17.    
  18. Functions:
  19.    - display_menu(): Displays a menu for the user to select a file extension.
  20.    
  21.        Extension Types and Descriptions:
  22.            1. .avif   - AV1 Image File Format, a next-generation image format designed for efficiency.
  23.            2. .bmp    - Bitmap Image File, a raster graphics image file format used to store bitmap digital images.
  24.            3. .eps    - Encapsulated PostScript, a graphics file format used for vector images.
  25.            4. .gif    - Graphics Interchange Format, a bitmap image format that supports animations and transparency.
  26.            5. .heic   - High Efficiency Image File Format, a file format for photos and images with better compression.
  27.            6. .ico    - Icon Image File, used for small graphics in the form of symbols, icons, etc.
  28.            7. .jpg    - Joint Photographic Experts Group, a widely used image format known for its lossy compression.
  29.            8. .jpeg   - JPEG File Interchange Format, similar to .jpg but used to describe the file format more explicitly.
  30.            9. .pdf    - Portable Document Format, a file format used to present documents in a manner independent of software, hardware, and operating systems.
  31.           10. .png    - Portable Network Graphics, a raster-graphics file format that supports lossless data compression.
  32.           11. .psd    - Photoshop Document, a file format used to create and edit raster graphics in Adobe Photoshop.
  33.           12. .raw    - Raw Image Data File, contains minimally processed data from a digital camera's image sensor.
  34.           13. .svg    - Scalable Vector Graphics, an XML-based vector image format for two-dimensional graphics.
  35.           14. .tga    - Truevision Targa Graphic, a raster graphics file format commonly used for storing high-quality images with full-color support and transparency.
  36.           15. .tiff   - Tagged Image File Format, a flexible raster graphics file format that supports lossless compression.
  37.           16. .webp   - WebP Image Format, a modern image format that provides superior lossless and lossy compression for images on the web.
  38.  
  39.    - main(): Main function to execute the renaming of files based on user input.
  40.  
  41. Usage:
  42.    - Run the script and follow the prompts to select a file extension for renaming files.
  43.  
  44. Additional Notes:
  45.    - Files with extensions .py and .txt are excluded from renaming.
  46.    - Directories are excluded from renaming.
  47.    - Choosing 'Remove Extensions' deletes current extensions from file names.
  48.    - Selecting 'Custom Extension' permits entering a preferred extension.
  49. """
  50.  
  51. import os
  52.  
  53. # Generate an ascii header
  54. def header():
  55.     print("""
  56.      ____  ____  ____  ____  ____  ____  ____  ____  ____
  57.     ||E ||||x ||||t ||||e ||||n ||||s ||||i ||||o ||||n ||
  58.     ||__||||__||||__||||__||||__||||__||||__||||__||||__||
  59.     |/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\|
  60.      ____  ____  ____  ____  ____  ____  ____  ____  ____
  61.     ||  ||||M ||||a ||||n ||||a ||||g ||||e ||||r ||||  ||
  62.     ||__||||__||||__||||__||||__||||__||||__||||__||||__||
  63.     |/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\||/__\\|            
  64.    """)
  65.  
  66. # Function to display the menu and get user selection
  67. def display_menu():
  68.     header()
  69.     print("[Select An Extension To Convert All Files In The Current Directory]\n")
  70.     print("\t 1. .avif\t\t 2. .bmp")
  71.     print("\t 3. .eps\t\t 4. .gif")
  72.     print("\t 5. .heic\t\t 6. .ico")
  73.     print("\t 7. .jpg\t\t 8. .jpeg")
  74.     print("\t 9. .pdf\t\t10. .png")
  75.     print("\t11. .psd\t\t12. .raw")
  76.     print("\t13. .svg\t\t14. .tga")
  77.     print("\t15. .tiff\t\t16. .webp")
  78.     print("\t17. [Remove Extensions]\t18. [Custom Extension]")
  79.     print("\n\t[Or... Type '0' To Exit]")
  80.    
  81.     while True:
  82.         choice = input("\nEnter the number corresponding to your choice: ")
  83.         print("_" * 80)
  84.         if choice == "1":
  85.             return ".avif"
  86.         elif choice == "2":
  87.             return ".bmp"
  88.         elif choice == "3":
  89.             return ".eps"
  90.         elif choice == "4":
  91.             return ".gif"
  92.         elif choice == "5":
  93.             return ".heic"
  94.         elif choice == "6":
  95.             return ".ico"
  96.         elif choice == "7":
  97.             return ".jpg"
  98.         elif choice == "8":
  99.             return ".jpeg"
  100.         elif choice == "9":
  101.             return ".pdf"
  102.         elif choice == "10":
  103.             return ".png"
  104.         elif choice == "11":
  105.             return ".psd"
  106.         elif choice == "12":
  107.             return ".raw"
  108.         elif choice == "13":
  109.             return ".svg"
  110.         elif choice == "14":
  111.             return ".tga"
  112.         elif choice == "15":
  113.             return ".tiff"
  114.         elif choice == "16":
  115.             return ".webp"
  116.         elif choice == "17":
  117.             return ""  # Empty string to remove extensions
  118.         elif choice == "18":
  119.             custom_extension = input("\nEnter the custom extension (without preceding dot): ")
  120.             print("_" * 80)
  121.             return f".{custom_extension}"
  122.         elif choice == "0":
  123.             print("\nExiting Program...   GoodBye!")
  124.             print("_" * 80)
  125.             exit()
  126.         else:
  127.             print("Invalid choice. Please try again.")
  128.  
  129. # Get the current working directory
  130. cwd = os.getcwd()
  131.  
  132. # Get the desired extension from the user
  133. new_extension = display_menu()
  134.  
  135. # Loop through all files in the directory
  136. for filename in os.listdir(cwd):
  137.     # Get the full path of the file
  138.     file_path = os.path.join(cwd, filename)
  139.  
  140.     # Check if the item is a file (not a directory), and is not a .py or .txt file
  141.     if os.path.isfile(file_path) and not filename.endswith((".py", ".txt")):
  142.         # Determine the new filename
  143.         if new_extension == "":
  144.             # Remove the extension
  145.             new_filename = os.path.splitext(filename)[0]
  146.         elif not os.path.splitext(filename)[1]:
  147.             # Only add the new extension if there is no extension currently
  148.             new_filename = filename + new_extension
  149.         else:
  150.             # Skip files that already have an extension if we are not removing extensions
  151.             continue
  152.         # Get the new file path
  153.         new_file_path = os.path.join(cwd, new_filename)
  154.         # Rename the file
  155.         os.rename(file_path, new_file_path)
  156.         print(f"Renamed {file_path} to {new_file_path}")
  157.  
  158. print()
  159. print("_" * 80)
  160. print("\nExiting Program...   GoodBye!")
  161. print("_" * 80)
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement