No history yet

File System Operations

Your Digital Filing Cabinet

Think of your computer's file system as a giant digital filing cabinet. It has drawers (directories or folders) and files inside those drawers. Python can open this cabinet and organize things for you, but it needs the right key. That key is the os module.

The os module provides a way of using operating system dependent functionality, like reading or writing to the file system.

To get started, you need to import it into your script. This single line gives you access to a whole suite of tools for interacting with files and directories.

import os

Finding Your Way Around

Before you can organize your files, you need to know where you are. When your Python script runs, it has a "current working directory" (CWD). This is the folder it's standing in, by default. To find out where that is, use os.getcwd().

# Get the current working directory
current_directory = os.getcwd()
print(f"I'm currently in: {current_directory}")

This will print the full path to your current location. What if you want to move to a different folder? You can change your directory with os.chdir().

Imagine you're in a /Users/yourname/Documents folder and want to move into a subfolder called projects. You would use os.chdir('projects').

Once you're in a directory, you'll probably want to see what's inside. The os.listdir() function returns a list of all files and folders in a given path. If you don't provide a path, it lists the contents of the current working directory.

# List everything in the current directory
contents = os.listdir()
print(contents)

# You can also specify a path
# contents_of_other_folder = os.listdir('/path/to/other/folder')

Creating and Removing Folders

Organizing files often means creating new folders. The os module gives you two ways to do this.

To create a single folder, use os.mkdir().

# Create a new directory named 'invoices'
os.mkdir('invoices')

This is simple, but it has a limitation: it can only create one folder at a time. If you try to create a folder inside another one that doesn't exist yet, like reports/2024, it will fail. For that, you need os.makedirs().

# Create a nested directory structure
os.makedirs('reports/2024/q1')

This command is more powerful, as it creates any necessary parent directories along the way.

Cleaning up is just as important. To remove an empty folder, use os.rmdir().

# Remove the 'invoices' directory
# This will only work if 'invoices' is empty
os.rmdir('invoices')

Just like mkdir, rmdir has a more powerful counterpart, os.removedirs(), which will remove nested empty directories.

Managing Individual Files

Now let's talk about the files themselves. You can create, rename, and delete them easily.

To create a new, empty file, a common Python trick is to open it in write mode ('w') and then immediately close it.

# Create a blank file named 'notes.txt'
open('notes.txt', 'w').close()

Renaming a file is straightforward with os.rename(). You just need to provide the original name and the new name.

# Rename 'notes.txt' to 'important_notes.txt'
os.rename('notes.txt', 'important_notes.txt')

Finally, to permanently delete a file, use os.remove(). Be careful with this one, as there's no undo button!

# Delete the file
os.remove('important_notes.txt')

With these commands, you have the fundamental tools to write scripts that can organize, clean, and manage your file system automatically.

Time to check your understanding.

Quiz Questions 1/7

Which module must be imported in Python to interact with the underlying operating system's file system?

Quiz Questions 2/7

Which line of Python code will display the full path of the directory your script is currently running in?

Now you know how to use Python to interact with your computer's file system. This is a foundational skill for automating all sorts of tasks.