Wednesday, May 19, 2021

Real Python: Build a Bulk File Rename Tool With Python and PyQt

Say you need to rename multiple files in your personal folder using a specific naming pattern. Doing that manually can be time-consuming and error-prone. So, you’re thinking of automating the file renaming process by building your own bulk file rename tool using Python. If so, then this tutorial is for you.

In this tutorial, you’ll learn how to:

  • Create the GUI for a bulk file rename tool using Qt Designer and PyQt
  • Use PyQt threads to offload the file renaming process and prevent freezing GUIs
  • Use pathlib to manage system paths and rename files
  • Update the GUI state according to the renaming process

By completing the project in this tutorial, you’ll be able to apply a rich set of skills related to PyQt, Qt Designer, PyQt threads, and working with file system paths using Python’s pathlib.

You can download the final source code for the bulk file rename tool you’ll build in this tutorial by clicking the link below:

Get the Source Code: Click here to get the source code you’ll use to build a bulk file rename tool with Python in this tutorial.

Demo: Bulk File Rename Tool With Python and PyQt

In this tutorial, you’ll build a bulk file rename tool to automate the process of renaming multiple files in a given directory in your file system. To build this application, you’ll use Python’s pathlib to manage the file renaming process and PyQt to build the application’s graphical user interface (GUI).

Here’s how your bulk file rename tool will look and work once you get to the end of this tutorial:

Once you finish building the application, you’ll be able to rename multiple files in your file system, a common task when you’re organizing your personal files and folders. In this example, the application focuses on images and Python files, but you can add other file types as you go.

Project Overview

The project you’ll build in this tutorial consists of a GUI application that loads multiple files from a given directory and allows you to rename all those files in one go using a predefined filename prefix and consecutive numbers. In this section, you’ll take a first look at the problem and a possible solution. You’ll also figure out how to lay out the project.

Laying Out the Project

To build your bulk file rename tool, you’ll create a few modules and packages and organize them in a coherent Python application layout. Your project’s root directory will look like this:

./rprename_project/
│
├── rprename/
│   │
│   ├── ui/
│   │   ├── __init__.py
│   │   ├── window.py
│   │   └── window.ui
│   │
│   ├── __init__.py
│   ├── app.py
│   ├── rename.py
│   └── views.py
│
├── README.md
├── requirements.txt
└── rprenamer.py

Here, rprename_project/ is the project’s root directory, where you’ll create the following files:

  • README.md provides a general project description and instructions on installing and running the application. Having a README.md file for your project is considered a best practice in programming, especially if you’re planning to release it as an open source solution.
  • requirements.txt provides the list of external dependencies for the project.
  • rprenamer.py provides an entry-point script to run the application.

Then you have the rprename/ directory that will hold a Python package with the following modules:

  • __init__.py enables rprename/ as a Python package.
  • app.py provides the PyQt skeleton application.
  • rename.py provides the file renaming functionalities.
  • views.py provides the application’s GUI and related functionalities.

The ui/ subdirectory will provide a package to store GUI-related code. It’ll contain the following files and modules:

  • __init__.py enables ui/ as a Python package.
  • window.py contains the Python code for the application’s main window. You’ll see how to generate this file using pyuic5.
  • window.ui holds a Qt Designer file that contains the code for the application’s main window in XML format.

Go ahead and create this directory structure with all the files and modules except for window.ui and window.py. You’ll see how to create these two files with Qt Designer and pyuic5 later in this tutorial.

To download the project’s directory structure, click the link below:

Get the Source Code: Click here to get the source code you’ll use to build a bulk file rename tool with Python in this tutorial.

Read the full article at https://realpython.com/bulk-file-rename-tool-python/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...