Python File System with OS and shutil Modules

September 08, 2025   6 Min Read 17

File & Directory Management, Automation, Copy/Move/Delete

What you'll Learn

  • What are os and shutil modules
  • How to interact with the file system using Python
  • Create, rename and delete files/directories
  • Navigate through directories
  • Copy, move and remove files using shutil
  • Automating repetitive file tasks
  • Best practices for safe file handling

Introduction

When working on projects, you'll often need to:

  • Check if a file exists
  • Create folders for saving output
  • Move or delete files automatically
  • Organize large datasets

Python's built-in os and shutil modules make these tasks simple and powerful.

The os Module – Working with Files and Directories

Get Current working directory


import os
print(os.getcwd())  # e.g., /Users/john/projects

List Files in a Directory


files = os.listdir(".")  # ".means current directory
print(files)

Create and Remove Directories


os.mkdir("new_folder")  # create new folder
os.rmdir("new_folder")  # remove empty folder

Rename Files or Folders


os.rename('old.txt', 'new.txt')

Check Path and File Existence


print(os.path.exists("data.txt"))  # True/False
print(os.path.isfile("data.txt"))  # Check if it's a file
print(os.path.isdir("my_folder"))  # Check if it's a folder

The shutil Module – High-Level File Operations

The shutil module gives you advanced operations like copy, move, and delete.


import shutil


shutil.copy("data.txt", "backup.txt")  # copy file
shutil.copytree("src_folder", "dest_folder")  # copy entire folder

Move Files


shutil.move("backup.txt", "archive/backup.txt")

Remove Directories


shutil.rmtree("old_folder")  # removes folder + all contents

⚠️ Be careful with rmtree() – it deletes everything inside permanently.

Example: File Organizer Script


import os, shutil


def organize_files(folder):
	for filename in os.listdir(folder):
		if filename.endswith(".txt"):
			shutil.move(os.path.join(folder, filename), os.path.join(folder, "Text_Files", filename))
		elif filename.endswith(".jpg"):
			shutil.move(os.path.join(folder, filename), os.path.join(folder, "Images", filename))

			
organize_files("downloads")

This script organizes files in a folder into Text_Files and Images directories.

🛠 Task for You

  1. Create a script that:
    • Scans a directory
    • Moves .pdf files into PDFs/
    • Moves .csv files into CSVs/
    • Deletes any .tmp files

Best Practices

  • Always check if a file/folder exists before modifying
  • Use with open(...) when reading/writing files
  • Avoid hardcoding paths – use os.path.join() for cross-platform compatibility
  • Be cautious with shutil.rmtree() as it deletes permanently

Summary

ModuleFeatureExample
osPath handling, mkdir, listdiros.listdir()
shutilCopy, move, delete directoriesshutil.copy(), shutil.rmtree()
SafetyCheck before deleting/movingos.path.exists("file.txt")

Leave a Reply