Topic 3: File System Operations

1. Introduction

The file system is a method and data structure that an operating system uses to manage files on disk drives. To manipulate these files (e.g., reading, writing, or deleting), Python provides a set of built-in functions and modules. In this section, we’ll deep dive into these operations.

2. The os Module

Python’s built-in os module provides a way to use operating system dependent functionality, like reading or writing to the file system.

  • Creating Directories:

    python
    import os os.mkdir("new_directory")

    For creating deep directories, use os.makedirs():

    python
    os.makedirs("new_directory/sub_directory")
  • Listing Directories:

    python
    for dirpath, dirnames, filenames in os.walk("."): print(f"Found directory: {dirpath}") for file_name in filenames: print(file_name)
  • Removing Directories:

    python
    os.rmdir("new_directory")

    Use os.removedirs() to remove multiple nested empty directories.

3. File Operations with os and shutil

  • Renaming Files:

    python
    os.rename("old_file.txt", "new_file.txt")
  • Removing Files:

    python
    os.remove("file_to_remove.txt")
  • Copying Files:

    The shutil module, also part of Python’s standard library, provides a higher-level file operations interface.

    python
    import shutil shutil.copy("source.txt", "destination.txt")
  • Moving Files:

    python
    shutil.move("source.txt", "destination_folder/")

4. File Properties

You can obtain meta-information about files using the os module:

  • Checking if a File or Directory Exists:

    python
    print(os.path.exists("filename.txt")) # Returns True or False
  • Size of a File:

    python
    size = os.path.getsize("filename.txt")
  • Checking if a Path is a File or Directory:

    python
    os.path.isfile("filename.txt") # True if it's a file os.path.isdir("directory_name") # True if it's a directory

5. Working with Paths

The os.path module provides methods to work with file paths:

  • Joining Paths:

    It’s safer to use os.path.join() to create paths as it ensures the correct path separators are used for the underlying operating system.

    python
    path = os.path.join("folder", "file.txt")
  • Splitting Paths:

    python
    head, tail = os.path.split("/path/to/file.txt") print(head) # /path/to print(tail) # file.txt

6. Conclusion

Python’s os and shutil modules offer a comprehensive suite of functions to manipulate the file system. These operations are crucial when automating tasks, managing datasets, or interacting with the underlying operating system. It’s essential to understand them thoroughly for various applications and scripts, ensuring smooth file operations across different platforms and environments.