Free Interactive Python Course

Python Tutorial — Learn Python Step by Step

Key takeaway: Master Python programming from scratch — variables, loops, functions, OOP, file handling, and libraries — with clear human-readable explanations and runnable code examples.

50+ Structured Lessons
Runnable Code Examples
100% Free No Registration
Updated October 2026

Python Directory and Files Management

Python provides the os, shutil, and pathlib modules for creating, reading, moving, and deleting files and directories.

Reading and Writing Files

# Writing to a file
with open("output.txt", "w") as f:
    f.write("Hello, World!\n")
    f.write("Second line\n")

# Reading entire file
with open("output.txt", "r") as f:
    content = f.read()
    print(content)

# Reading line by line (memory efficient for large files)
with open("output.txt") as f:
    for line in f:
        print(line.strip())

# Appending to a file
with open("output.txt", "a") as f:
    f.write("Appended line\n")

# Reading into a list of lines
with open("output.txt") as f:
    lines = f.readlines()
    print(lines)  # ["Hello, World!\n", "Second line\n", ...]

Working with Directories (os module)

import os

# Current working directory
print(os.getcwd())  # /home/user/project

# List directory contents
files = os.listdir(".")
print(files)  # ["main.py", "data", "README.md"]

# Create a directory
os.mkdir("new_folder")           # single directory
os.makedirs("path/to/nested", exist_ok=True)  # nested, no error if exists

# Check if path exists
print(os.path.exists("output.txt"))     # True
print(os.path.isfile("output.txt"))     # True
print(os.path.isdir("new_folder"))      # True

# Get file information
size = os.path.getsize("output.txt")    # file size in bytes
print(f"File size: {size} bytes")

# Rename a file or directory
os.rename("old_name.txt", "new_name.txt")

# Remove file and directory
os.remove("temp.txt")            # delete a file
os.rmdir("empty_folder")         # delete empty directory only

pathlib — Modern Path Handling (Python 3.4+)

from pathlib import Path

# Create path objects
current = Path(".")
home = Path.home()
config = home / ".config" / "myapp" / "settings.json"

# Path properties
p = Path("/home/user/documents/report.pdf")
print(p.name)       # "report.pdf"
print(p.stem)       # "report"
print(p.suffix)     # ".pdf"
print(p.parent)     # /home/user/documents
print(p.parts)      # ("/", "home", "user", "documents", "report.pdf")

# Check existence
print(config.exists())     # True/False
print(config.is_file())    # True/False
print(config.is_dir())     # False

# Create directories
Path("data/raw").mkdir(parents=True, exist_ok=True)

# Read and write files
path = Path("greeting.txt")
path.write_text("Hello from pathlib!")
content = path.read_text()
print(content)  # "Hello from pathlib!"

# Iterate directory contents
for item in Path(".").iterdir():
    print(f"{"DIR " if item.is_dir() else "FILE"} {item.name}")

# Glob pattern matching
for py_file in Path("src").glob("**/*.py"):  # recursive
    print(py_file)

Copying, Moving, and Deleting (shutil)

import shutil

# Copy a file
shutil.copy("source.txt", "backup.txt")          # copy file
shutil.copy2("source.txt", "backup.txt")         # copy with metadata

# Copy entire directory
shutil.copytree("src_folder", "dst_folder")

# Move file or directory
shutil.move("old_location/file.txt", "new_location/file.txt")

# Delete directory and all contents (be careful!)
shutil.rmtree("folder_to_delete")

# Get disk usage
total, used, free = shutil.disk_usage("/")
print(f"Free space: {free // (1024**3)} GB")

Practical Examples

# Find all files larger than 1MB
from pathlib import Path

def find_large_files(directory, min_size_mb=1):
    min_bytes = min_size_mb * 1024 * 1024
    large_files = []
    for f in Path(directory).rglob("*"):
        if f.is_file() and f.stat().st_size > min_bytes:
            size_mb = f.stat().st_size / (1024 * 1024)
            large_files.append((f, size_mb))
    return sorted(large_files, key=lambda x: x[1], reverse=True)

for path, size in find_large_files(".", min_size_mb=0.5):
    print(f"{size:.1f} MB - {path}")

# Organize files by extension
import shutil
from pathlib import Path

def organize_downloads(folder):
    categories = {
        "Images": [".jpg", ".png", ".gif", ".svg"],
        "Documents": [".pdf", ".docx", ".txt", ".xlsx"],
        "Videos": [".mp4", ".avi", ".mkv"],
        "Code": [".py", ".js", ".html", ".css"],
    }
    for file in Path(folder).iterdir():
        if file.is_file():
            for category, extensions in categories.items():
                if file.suffix.lower() in extensions:
                    dest = Path(folder) / category
                    dest.mkdir(exist_ok=True)
                    shutil.move(str(file), str(dest / file.name))
                    break
  • Always use with open() to ensure files are properly closed after use.
  • Prefer pathlib.Path over os.path for cleaner, more readable path code.
  • Use shutil for copying, moving, and deleting entire directories.
  • Use exist_ok=True with mkdir() to avoid errors if directory exists.
  • Use .rglob("pattern") or .glob("**/pattern") for recursive file search.

Python Programming Tutorial — Learn Python in a practical, step-by-step way. According to the official Python documentation, Python emphasizes code readability and simplicity, making it ideal for beginners.

Related tools: Python Online Compiler | Python Examples | JavaScript Tutorial | Java Tutorial

Python Career & Learning Guide

Python is the world's most popular programming language for beginners, data science, AI/ML, web development, and automation. This tutorial teaches Python step-by-step with clear explanations and runnable code examples in our free Python Compiler.

Why Learn Python in 2026?

  • #1 Most Popular Language: Ranked first on TIOBE, Stack Overflow, and GitHub.
  • AI and Data Science: The primary language for machine learning (TensorFlow, PyTorch, scikit-learn) and data analysis (Pandas, NumPy).
  • Web Development: Django and Flask power scalable backends at companies like Instagram and Spotify.
  • Automation & Scripting: Automate files, emails, web scraping, and data processing in minutes.

Python vs Other Languages

FeaturePythonJavaJavaScriptC++
SyntaxVery clean, readableVerboseModerateComplex
TypingDynamic, strongStatic, strongDynamic, weakStatic, strong
Best ForAI/ML, Data, WebEnterprise, AndroidWeb Frontend/BackendSystems, Games
Learning Time2–4 weeks basics4–6 weeks basics3–4 weeks basics8–12 weeks basics

Frequently Asked Questions

Do I need prior programming experience to learn Python?

No! Python is specifically designed to be beginner-friendly. This tutorial starts from zero and builds up gradually.

Which Python version should I use?

Python 3.10+ is recommended. All examples in this tutorial use Python 3 syntax.

Is this Python tutorial completely free?

Yes, 100% free with no registration or sign-up required. Access all lessons and code examples anytime.