Python for Beginners: Organizing Virtual Space using Python

Picture this: a cluttered virtual space with random files scattered all over your PC. Now you can manually sort them out but programmers always want to get creative for doing simple stuff, don’t we? In today’s blog post, we’ll dive into a hands-on exercise that not only instills a sense of accomplishment but also enhances your confidence in Python. And the best part? This mini-project is lies under the category of “Python for beginners” and if you are also a beginner, looking for a hands-on project, this one could be something that you would enjoy.

By the end of this blog, you will be able to run your Python automation script and watch it as it dynamically creates folders with generic names, efficiently sorting files into their designated spaces.

python for beginners

Approaching the Task

We start by contemplating how we can approach this task. To organize the files, 3 main questions need to be addressed:

  1. On what parameters should the files be organized?
  2. What kind of files do we want to organize?
  3. Which directory do we want to organize?

1. On what parameters should the files be organized?

We can organize files based on various factors such as name, creation date, modified date and size. We can also manage files based on the type/extension of a file. That is where the next question comes into the picture.

2. What kind of files do we want to organize?

There are many types of files we might find in our download folder. There can be music, video, images, zip, executables, documents, and many more.

3. Which directory we want to organize?

It is a common scenario where a folder contains subfolders and hence, it becomes important to ask if those should be taken into consideration as well. For example, a traditional project workspace contains folders like build, install, and src, where the src folder contains further sub-directories of different modules of the programming project. Organizing such a workspace based on file type is not a good idea because it can mess up the entire project. Thus, we will mostly focus on the root directory.

Understanding File Properties

To effectively organize files, it is essential to first understand their properties, such as file type, size, and date of creation. These properties provide valuable information for sorting and filtering files. In Python, we can utilize the versatile os module to access and manipulate these properties programmatically.

Let us take a look at an example that demonstrates how to retrieve the file properties of a given file using the os.path method:

import os

file_path = 'path/to/file.txt'

# Get file type
file_type = os.path.splitext(file_path)[1]

# Get file size in bytes
file_size = os.path.getsize(file_path)

# Get date of creation
creation_date = os.path.getctime(file_path)
print(f"File Type: {file_type}")
print(f"File Size: {file_size} bytes")
print(f"Creation Date: {creation_date}")

In the above example, we use the os.path.splitext() function to extract the file extension, os.path.getsize() function to get the file size in bytes, and os.path.getctime() function to obtain the creation date of the file.

Sorting Files

Sorting files is a fundamental aspect of file organization. Python offers various techniques to sort files based on different properties. Let’s explore how we can sort files based on file type, size, and date of creation using Python.

Sorting by File Type

To sort files by their types, we can use the sorted() function along with the key parameter and a lambda function. Here’s an example that demonstrates how to sort files in a directory based on their file types:

import os

directory = 'path/to/files/'
files = os.listdir(directory)
sorted_files = sorted(files, key=lambda x: os.path.splitext(x)[1])

for file in sorted_files:
  print(file)

In this example, the os.listdir() function retrieves a list of files in the specified directory. The sorted() function sorts the files based on their file extensions, extracted using the os.path.splitext() function. Finally, we iterate through the sorted files and print their names.

Sorting by File Size

To sort files based on their sizes, we can utilize the sorted() function with the key parameter and the os.path.getsize() function. Here’s an example that demonstrates sorting files by size in descending order:

import os

directory = 'path/to/files/'
files = os.listdir(directory)
sorted_files = sorted(files, key=lambda x: os.path.getsize(os.path.join(directory, x)), reverse=True)

for file in sorted_files:
  print(file)

In this example, the os.path.getsize() function is used within the lambda function to retrieve the size of each file.

The reverse=True argument ensures that the files are sorted in descending order of size.

Sorting by Date of Creation

To sort files based on their dates of creation, we can again use the sorted() function with the key parameter and the os.path.getctime() function. Here’s an example that demonstrates sorting files by creation date in ascending order:

import os

directory = 'path/to/files/'
files = os.listdir(directory)
sorted_files = sorted(files, key=lambda x:
os.path.getctime(os.path.join(directory, x)))

for file in sorted_files:
  print(file)

In this example, the os.path.getctime() function retrieves the creation time of each file. The files are sorted in ascending order by creation date.

Filtering Files

Filtering files allows us to extract specific subsets of files based on defined criteria. Python’s list comprehension feature offers a concise and powerful way to filter files efficiently. Let’s explore how we can filter files based on specific attributes using Python.

Filtering by File Extension

To filter files based on their extensions, we can use a list comprehension. Here’s an example that demonstrates how to filter files in a directory to only include text files:

import os

directory = 'path/to/files/'

files = os.listdir(directory)

text_files = [file for file in files if file.endswith('.txt')]

for file in text_files:
  print(file)

In this example, the list comprehension filters the files by checking if each file name ends with the ‘.txt’ extension.

Filtering by File Size

To filter files based on their sizes, we can combine list comprehensions with the os.path.getsize() function. Here’s an example that filters files to only include those larger than a specified size:

import os
directory = 'path/to/files/'
min_size = 1024  # Minimum file size in bytes
files = os.listdir(directory)
filtered_files = [file for file in files if os.path.getsize(os.path.join(directory, file)) > min_size]
for file in filtered_files:
    print(file)

In this example, the list comprehension filters the files by comparing their sizes with the min_size variable.

Moving Files

Once files are sorted and filtered, it’s often necessary to relocate them to different directories for improved organization. Python’s shutil module provides us with convenient functions for moving files. Let’s see how we can move files from one directory to another using Python.

import os
import shutil

source_directory = 'path/to/source/'
destination_directory = 'path/to/destination/'
files = os.listdir(source_directory)

for file in files:
  source_path = os.path.join(source_directory, file)
  destination_path = os.path.join(destination_directory, file)
  shutil.move(source_path, destination_path)

In this example, we iterate through the files in the source directory, obtain the source and destination paths for each file, and use the shutil.move() function to move the files to the specified destination directory.

Practical Example: Automating File Organization

Now let’s walk through a practical example that demonstrates how to automate file organization based on specific criteria. For instance, we can create a script that separates images, documents, and music files into dedicated folders.

Create a new file called ‘organize.py’ in a folder of your choice.

Place the following code in the file and update the directory paths to fit your needs:

import os
import shutil
# directory paths
source_directory = 'path/to/files/'
image_directory = 'path/to/images/'
document_directory = 'path/to/documents/'
music_directory = 'path/to/music/'
# gather the list of files from the source directory
files = os.listdir(source_directory)
# iterate through the files in the source directory
for file in files:
    # move images to the image directory
    if file.endswith(('.jpg', '.png', '.gif')):
        shutil.move(os.path.join(source_directory, file), image_directory)
    # move documents to the document directory
    elif file.endswith(('.pdf', '.doc', '.txt')):
        shutil.move(os.path.join(source_directory, file), document_directory)
    # move music to the music directory
    elif file.endswith(('.mp3','.wav', '.flac')):
        shutil.move(os.path.join(source_directory, file), music_directory)

Save the file.

Run the file using the command:

python3 organize.py

You could use Windows Task Scheduler to run this script on a set schedule automatically.

Conclusion

In this small project, we managed to organize files and folders using a Python programming. Such hands-on projects are great to build something, get creative and to increase your confidence in Python programming language. There are some other interesting articles I’ve published like:
Building a Python-Powered GPS Tracking Device for Bikes,
Exploring the Power of Computer Vision: A Beginner’s Guide to OpenCV in Python
Accessing Car data using Python
A Step-by-Step Guide to Reading MPU6050 Data with Raspberry Pi and Python and
Mastering Efficiency: 5 Python Automation Applications to Revolutionize Your Routine.

Go check them out, pick anyone (or maybe all) of them, and get your fingers hit that keyboard! I hope you enjoyed this blog. Have you any other project ideas that you would want to share? Come get in touch with me on my social media:

If you never want to miss such interesting posts, do subscribe to my *FREE* monthly newsletter:

Leave a Reply