4
Lesson 4
Working with file paths
Objective
By the end of this lesson, students will understand the concept of file paths, including the differences between absolute and relative paths. They will also learn to navigate the file system using Python’s 'os' and 'pathlib' modules.
1. Introduction to file paths:
In programming, file paths are essential for locating and accessing files on a computer. Python uses file paths to specify where a file is stored. There are two main types of paths in Python:
By the end of this lesson, students will understand the concept of file paths, including the differences between absolute and relative paths. They will also learn to navigate the file system using Python’s 'os' and 'pathlib' modules.
1. Introduction to file paths:
In programming, file paths are essential for locating and accessing files on a computer. Python uses file paths to specify where a file is stored. There are two main types of paths in Python:
- Absolute paths: Provide the complete location of a file or folder from the root directory.
- Relative paths: Define the location of a file relative to the current working directory.
Understanding these paths is crucial for performing file operations and navigating the file system effectively.
2. Absolute paths:
Absolute paths start from the root directory and specify the full path to a file or folder. They are independent of the current working directory.
# Absolute path on Windows file_path = "C:\\Users\\Username\\Documents\\example.txt" # Absolute path on MacOS or Linux file_path = "/home/username/Documents/example.txt"
3. Relative paths:
Relative paths specify the location of a file relative to the current working directory. They are generally more flexible than absolute paths as they allow programs to be portable across different systems and environments.
# Relative path in the same directory file_path = "example.txt" # Relative path in a subdirectory file_path = "folder/example.txt" # Moving up one directory file_path = "../example.txt"
4. Navigating Paths with the 'os' Module:
The 'os' module in Python provides tools for interacting with the operating system, including functions for path manipulation.
Getting the current working directory:
import os current_dir = os.getcwd() print("Current Directory:", current_dir)
Joining paths:
file_path = os.path.join("folder", "example.txt") print("Joined Path:", file_path)
Checking if a path exists:
exists = os.path.exists("example.txt") print("File Exists:", exists)
5. Navigating paths with the 'pathlib' module
The 'pathlib' module provides an object-oriented approach for working with file paths and is preferred for modern Python code.
Creating paths with 'pathlib.Path':
from pathlib import Path path = Path("folder/example.txt") print("Path:", path)
Checking if a path exists:
print("File Exists:", path.exists())
Getting the parent directory:
parent_dir = path.parent print("Parent Directory:", parent_dir)
Getting the file name and extension:
print("File Name:", path.name) print("File Extension:", path.suffix)
6. Practical examples and exercises:
Exercise 1: Absolute and relative paths
1. Write a Python script that prints the absolute path of the current working directory.
2. Create a relative path to a file within the current directory and print it.
Exercise 2: Navigating with 'os' and 'pathlib'
1. Use 'os.path.join()' to create a path to a file in a subfolder called "data."
2. Write a script using 'pathlib.Path' to check if a file named "example.txt" exists in the current directory.
Exercise 3: Path manipulation with 'pathlib'
1. Given a path, write code to print the file’s directory, name, and extension separately using pathlib.
Conclusion
In this lesson, students learned about absolute and relative paths in Python. They also explored how to navigate file paths using the 'os' and 'pathlib' modules. Mastering these techniques is essential for file handling, enabling students to write versatile and system-agnostic code.