Automating File Organisation With Python: A File Mover Script
Introduction
As part of my Python Web Development Career Track with CodingNomads, I wrote a Python script to move screenshots into a new directory in a given directory. This moves .png files from a general directory into a new subdirectory, automating file management.
This project uses the pathlib module, to implement path manipulation, iteration, conditional logic, and basic filesystem operations with Python.
This article provides a project walkthrough, including:
- Project requirements
- Details when implementing code
- A walkthrough with explanations
- Lessons learnt
- Potential project improvements
Project Concept
This script solves the problem of managing mixed file types in a single directory. This is because manually sorting files by their type can be tedious, especially when managing larger volumes.
Key Features
- A base directory containing multiple file types (.pdf, .txt, .png)
- A new subdirectory, png_files, is created for storing .png files
- The script iterates through the files in the base directory, only moving .png files
- Other file types remain unmoved
This approach uses path manipulation, conditional filtering, and file operations via Python's pathlib module.
Project Structure
The file tree for this project is below:
.
├── mover.py
└── moving_files
├── example.pdf
├── example.txt
└── png_files
├── example_one.png
├── example_three.png
└── example_two.png
- mover.py: the main project script
- moving_files: the directory containing the files to be processed
- png_files: the destination subdirectory for .png files
Implementation
This project was built using a single Python script. The following sections describe its implementation.
1.Importing Dependencies
import pathlib
from pathlib import Path
This uses the pathlib
module to provide an object-oriented interface for filesystem paths. This uses Path
objects for path construction, iteration, and manipulation.
2.Defining the Target Directory
folder_directory = pathlib.Path('/Users/francescapanteli/Desktop/CodingNomads-python-101/codingnomads/projects/mover')
folder_directory
specifies the directory of the files to be moved. Using Path
objects for this allows for cross-platform path handling.
3.Subdirectory Initialisation
new_folder = folder_directory / "png_files"
new_folder.mkdir(exist_ok=True)
This section of the code creates a subdirectory called png_files
, to store the .png
files in the previously specified directory. The parameter exist_ok=True
prevents the generation of an error that shows if this directory already exists. This ensures that the script can be run multiple times without generating the error.
4.Iterating and Filtering Files
for file in folder_directory.iterdir():
if file.suffix == ".png":
new_file_path = new_folder / file.name
file.rename(new_file_path)
-
folder_directory.iterdir()
iterates over all files in the directory -
file.suffix
checks the file extension -
.png
files are moved to thepng_files
subdirectory usingfile.rename(new_file_path)
- This ensures that other file types (
.pdf
,.txt
, etc.) remain unmoved
Example Code Implementation
Before running the script, the moving_files
directory contains mixed file types. After executing mover.py
, all .png files are moved into the png_files
directory. This automation removes the need to manually move these files.
Lessons Learnt
This project uses the following Python concepts:
- Pathlib and Path Objects: to navigate and manipulate file paths
-
Iteration: looping over directory contents using
iterdir()
- Conditional Logic: selecting files based on their extension
-
File Operations: moving files by using the
rename()
method - Automation: applying Python scripts to automate repetitive tasks
Potential Improvements
The script can be extended in several ways:
-
Command-line Arguments: using
argparse
, to allow dynamic directory and file type input - Error Handling: adding checks for missing directories, permission issues, or filename conflicts
- Logging: maintaining a record of moved files
-
Multiple File Types: extending functionality to organise
.pdf
,.txt
,.jpg
, etc
Source Code
This project is viewable on GitHub, at: https://github.com/franpanteli/CodingNomads-python-101/blob/main/labs/projects/mover/mover.py
Top comments (1)
Fantastic work, @fran_panteli!