Introduction: Why Automate with Python?

In modern work environments, repetitive tasks consume a significant amount of time and energy. How great would it be if you could automate daily repetitive tasks like organizing Excel files, sending emails, backing up files, and collecting data? Python is the most suitable programming language for such automation tasks.

Here's why Python is optimal for automation:

  • Easy Syntax: With intuitive syntax similar to English, even programming beginners can learn quickly.
  • Rich Libraries: Libraries exist for almost every task including file processing, web scraping, Excel manipulation, and email sending.
  • Cross-Platform: Works identically on Windows, Mac, and Linux.
  • Active Community: When problems arise, you can find solutions for most issues through search.
  • Free: As open source, anyone can use it for free.

In this series, we will learn practical automation techniques using Python step by step. In this first part, we will cover Python installation, development environment setup, basic syntax review, and writing your first automation script.

1. Installing Python

1.1 Installing Python on Windows

Here's how to install Python on Windows:

  1. Visit the Python official website.
  2. Click the "Download Python 3.x.x" button to download the latest version.
  3. Run the downloaded installation file.
  4. Important! Make sure to check the "Add Python to PATH" checkbox.
  5. Click "Install Now" to complete the installation.

To verify the installation, open Command Prompt (CMD) and enter the following commands:

python --version
pip --version

If version information is displayed, the installation was successful.

1.2 Installing Python on Mac

Mac comes with Python 2 pre-installed, but you need to install Python 3 separately:

Method 1: Using Official Installer

  1. Download the macOS installer from the Python official website.
  2. Run the downloaded .pkg file to install.

Method 2: Using Homebrew (Recommended)

# Install Homebrew (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Python
brew install python

1.3 Installing Python on Linux

Most Linux distributions come with Python pre-installed. If you need a newer version:

Ubuntu/Debian:

sudo apt update
sudo apt install python3 python3-pip python3-venv

CentOS/RHEL:

sudo yum install python3 python3-pip

Fedora:

sudo dnf install python3 python3-pip

2. Setting Up Virtual Environments

Virtual environments are tools that create independent Python environments for each project. Why do we need virtual environments?

  • You can use different versions of packages for each project.
  • You can keep the system Python clean.
  • You can clearly manage project dependencies.

2.1 venv (Built-in)

The venv module is included by default in Python 3.3 and later:

# Create virtual environment
python -m venv myenv

# Activate on Windows
myenv\Scripts\activate

# Activate on Mac/Linux
source myenv/bin/activate

# Deactivate
deactivate

When the virtual environment is activated, (myenv) will appear before the prompt.

2.2 virtualenv

virtualenv is a third-party tool that provides more features:

# Install virtualenv
pip install virtualenv

# Create virtual environment
virtualenv myenv

# Create with specific Python version
virtualenv -p python3.11 myenv

3. pip and Package Management

pip is Python's package manager. You can easily install and manage necessary libraries.

3.1 Basic Commands

# Install package
pip install requests

# Install specific version
pip install requests==2.28.0

# Upgrade package
pip install --upgrade requests

# Uninstall package
pip uninstall requests

# List installed packages
pip list

# Show package info
pip show requests

3.2 Using requirements.txt

Managing project dependencies in a file makes it easy to install the same packages in different environments:

# Save currently installed packages to requirements.txt
pip freeze > requirements.txt

# Install all packages from requirements.txt
pip install -r requirements.txt

requirements.txt example:

requests==2.31.0
beautifulsoup4==4.12.2
openpyxl==3.1.2
python-dotenv==1.0.0

4. VS Code Development Environment Setup

Visual Studio Code is the most popular free editor for Python development.

4.1 Installing VS Code

  1. Download from the VS Code official website.
  2. Run the installer appropriate for your operating system.

4.2 Essential Extensions

Open VS Code and install the following from the Extensions tab:

  • Python (Microsoft): Python language support, debugging, linting
  • Pylance (Microsoft): Enhanced code completion and type checking
  • Python Indent: Automatic indentation support
  • autoDocstring: Automatic docstring generation

4.3 VS Code Settings

Adding the following settings to settings.json will provide a better development experience:

{
    "python.defaultInterpreterPath": "python",
    "python.formatting.provider": "black",
    "editor.formatOnSave": true,
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true
}

5. Python Basic Syntax Review

Let's quickly review the core syntax needed for writing automation scripts.

5.1 Variables and Data Types

# Basic data types
name = "Python"           # String (str)
version = 3.12            # Float (float)
count = 100               # Integer (int)
is_active = True          # Boolean (bool)

# Collection data types
fruits = ["apple", "banana", "cherry"]    # List
coordinates = (10, 20)                     # Tuple
person = {"name": "John", "age": 30}       # Dictionary
unique_numbers = {1, 2, 3, 4, 5}          # Set

# Type checking
print(type(name))  # <class 'str'>

5.2 Conditionals

age = 25

if age < 18:
    print("Minor")
elif age < 65:
    print("Adult")
else:
    print("Senior")

# Ternary operator
status = "Adult" if age >= 18 else "Minor"

5.3 Loops

# for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# Using range
for i in range(5):
    print(i)  # 0, 1, 2, 3, 4

# Using enumerate (index and value simultaneously)
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

# while loop
count = 0
while count < 5:
    print(count)
    count += 1

# List comprehension
squares = [x**2 for x in range(10)]
even_numbers = [x for x in range(20) if x % 2 == 0]

5.4 Functions

# Basic function
def greet(name):
    return f"Hello, {name}!"

# Default parameter
def greet_with_time(name, time="morning"):
    return f"Good {time}, {name}!"

# Variable arguments
def sum_all(*args):
    return sum(args)

# Keyword arguments
def create_profile(**kwargs):
    return kwargs

# Lambda function
square = lambda x: x ** 2
add = lambda x, y: x + y

# Usage examples
print(greet("John"))
print(sum_all(1, 2, 3, 4, 5))
print(create_profile(name="Jane", age=25, city="New York"))

5.5 Exception Handling

try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero.")
except Exception as e:
    print(f"Error occurred: {e}")
else:
    print("Executed successfully.")
finally:
    print("Always executed.")

6. Writing Your First Automation Script

Now let's create a practical automation script using what we've learned. This script organizes files in a specified folder by their extensions.

6.1 Project Structure

file_organizer/
├── organize.py
├── requirements.txt
└── README.md

6.2 Script Code

"""
File Organization Automation Script
Organizes files in a specified folder by their extensions.
"""

import os
import shutil
from datetime import datetime

# Extension to folder mapping
EXTENSION_FOLDERS = {
    # Images
    '.jpg': 'Images',
    '.jpeg': 'Images',
    '.png': 'Images',
    '.gif': 'Images',
    '.bmp': 'Images',
    '.svg': 'Images',

    # Documents
    '.pdf': 'Documents',
    '.doc': 'Documents',
    '.docx': 'Documents',
    '.xls': 'Documents',
    '.xlsx': 'Documents',
    '.ppt': 'Documents',
    '.pptx': 'Documents',
    '.txt': 'Documents',

    # Videos
    '.mp4': 'Videos',
    '.avi': 'Videos',
    '.mkv': 'Videos',
    '.mov': 'Videos',

    # Music
    '.mp3': 'Music',
    '.wav': 'Music',
    '.flac': 'Music',

    # Archives
    '.zip': 'Archives',
    '.rar': 'Archives',
    '.7z': 'Archives',
    '.tar': 'Archives',
    '.gz': 'Archives',

    # Programs
    '.exe': 'Programs',
    '.msi': 'Programs',
    '.dmg': 'Programs',
}

def get_folder_for_extension(extension):
    """Returns the folder name for the given extension."""
    return EXTENSION_FOLDERS.get(extension.lower(), 'Others')

def organize_files(source_folder):
    """
    Organizes files in the specified folder by extension.

    Args:
        source_folder: Path to the folder to organize

    Returns:
        Number of files organized
    """
    if not os.path.exists(source_folder):
        print(f"Error: Folder '{source_folder}' does not exist.")
        return 0

    organized_count = 0

    # Iterate through files in the folder
    for filename in os.listdir(source_folder):
        file_path = os.path.join(source_folder, filename)

        # Skip folders
        if os.path.isdir(file_path):
            continue

        # Extract extension
        _, extension = os.path.splitext(filename)

        if not extension:
            continue

        # Determine target folder
        target_folder_name = get_folder_for_extension(extension)
        target_folder = os.path.join(source_folder, target_folder_name)

        # Create target folder if it doesn't exist
        if not os.path.exists(target_folder):
            os.makedirs(target_folder)
            print(f"Folder created: {target_folder_name}")

        # Move file
        target_path = os.path.join(target_folder, filename)

        # Handle duplicate filenames
        if os.path.exists(target_path):
            name, ext = os.path.splitext(filename)
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            new_filename = f"{name}_{timestamp}{ext}"
            target_path = os.path.join(target_folder, new_filename)

        shutil.move(file_path, target_path)
        print(f"Moved: {filename} -> {target_folder_name}/")
        organized_count += 1

    return organized_count

def main():
    """Main execution function"""
    print("=" * 50)
    print("File Organization Automation Script")
    print("=" * 50)

    # Input folder path to organize
    source_folder = input("Enter the folder path to organize: ").strip()

    if not source_folder:
        # Default: Current user's Downloads folder
        source_folder = os.path.join(os.path.expanduser("~"), "Downloads")
        print(f"Using default path: {source_folder}")

    # Confirmation
    confirm = input(f"Organize folder '{source_folder}'? (y/n): ")

    if confirm.lower() != 'y':
        print("Cancelled.")
        return

    # Execute file organization
    print("\nStarting file organization...\n")
    count = organize_files(source_folder)

    print("\n" + "=" * 50)
    print(f"Complete! {count} files have been organized.")
    print("=" * 50)

if __name__ == "__main__":
    main()

6.3 Running the Script

# Run the script
python organize.py

7. Conclusion and Preview of Next Part

In this part, we laid the foundation for Python automation. Here's a summary of what we learned:

  • Why Python is suitable for automation
  • How to install Python on Windows, Mac, and Linux
  • The necessity and usage of virtual environments (venv, virtualenv)
  • Package management with pip
  • VS Code development environment setup
  • Python basic syntax review
  • Writing your first automation script

Next Part Preview: In Python Automation Master Part 2, we will dive deep into file and folder automation. We'll explore advanced file processing techniques using the os module, pathlib, and shutil, and create practical file organization projects together!