Python Tutorial: Project - File Organizer 📁🔄

beginner
7 min

Python Tutorial: Project - File Organizer 📁🔄

Welcome to the File Organizer project tutorial! In this lesson, you'll learn Python basics by building a simple file organizer application.

By the end of this project, you'll have a practical understanding of essential Python concepts, including:

  • Variables and data types
  • File input and output
  • Conditional statements
  • Loops
  • Functions
  • List comprehensions

Getting Started 👋

Before diving in, let's ensure you have the following prerequisites:

  • Python 3 installed on your system
  • A text editor to write and save your Python code

Project Overview 🤝

Our goal is to create a command-line application that helps users organize their files by moving them to their respective directories based on file extensions. For instance, moving .txt files to a text_files directory and .jpg files to an images directory.

Variables and Data Types 💡

Variables are used to store data in your program. Here's an example:

python
my_variable = "Hello, World!" print(my_variable)

Python has various data types, including:

  • string (immutable sequence of characters)
  • integer (whole numbers)
  • float (decimal numbers)
  • boolean (True/False values)
  • list (ordered, mutable collection of items)
  • tuple (ordered, immutable collection of items)
  • dictionary (unordered collection of key-value pairs)

File Input and Output 📝

Let's read the files in our current directory:

python
import os files = os.listdir() print(files)

And let's create a new file:

python
open("new_file.txt", "w").close()

Conditional Statements ✅

Conditional statements allow your code to make decisions. Here's an example using an if statement:

python
file_extension = "txt" if file_extension == "txt": print("This is a text file.")

Loops 🎯

Loops allow you to repeat blocks of code. We'll use a for loop to move files to their respective directories:

python
source_directory = "." destination_directories = {"txt": "text_files", "jpg": "images"} for file in os.listdir(source_directory): extension = file.split(".")[-1] if extension in destination_directories: destination_path = os.path.join(source_directory, destination_directories[extension]) source_path = os.path.join(source_directory, file) os.rename(source_path, os.path.join(destination_path, file))

Functions 📝

Functions help organize your code and make it more readable. Here's an example:

python
def greet(name): print(f"Hello, {name}!") greet("Alice")

List Comprehensions 🎯

List comprehensions are a concise way to create lists. Here's an example:

python
numbers = [1, 2, 3, 4, 5] squared_numbers = [number ** 2 for number in numbers] print(squared_numbers)

Putting it all Together 🤝

Now that you've learned the basics, let's put everything together to build our file organizer.

Quick Quiz
Question 1 of 1

What does the `os.listdir()` function do?

Quick Quiz
Question 1 of 1

What is the purpose of the `os.path.join()` function?

Quick Quiz
Question 1 of 1

How do we move a file using Python?