This Python script is designed to rename image files in a specific directory. It searches for files with common image extensions (.png
, .jpg
, .jpeg
, .gif
, .bmp
, .tiff
) and renames them sequentially as "photo-001.png", "photo-002.png", and so on.
The code performs the following steps:
- Imports the
os
module to interact with the operating system. - Retrieves the current directory where the Python file is located.
- Lists all the files in the directory.
- Initialises a counter for the file names.
- Iterates over each file in the directory.
- Checks if the file is an image based on the extension.
- Creates a new file name in the format "photo-XXX.png", where XXX is a three-digit number incremented for each renamed file.
- Constructs the full path of the current file and the new file.
- Renames the file using the
os.rename()
function. - Increments the counter.
- Displays a message indicating that the renaming is complete.
- Ensure you have Python installed on your system.
- Save the Python script in a directory that contains the image files you want to rename.
- Open a terminal or command prompt and navigate to the directory where the script is located.
- Run the script by typing
python image-renamer.py
in the terminal. - The script will rename all the image files in the directory and display a completion message.
If this script has been helpful for you, consider making a donation to support our work:
- $USDT (TRC-20): TP6zpvjt2ZNGfWKPevfp65ZrcbKMWSQXDi
Your donations help us continue developing useful and innovative tools.
Thank you for using our script!