Skip to content

File and Directory Operations

If we want to operate on files and directories, we can use various commands provided by the operating system in the command line, such as dir, cp, etc.

In Python, we can perform these file and directory operations using the built-in os module, which directly calls the operating system's interface functions.

Let's take a look at how to use the basic functionality of the os module in the Python interactive command line:

python
import os
print(os.name)  # Operating system type
  • If the output is 'posix', it indicates that the system is Linux, Unix, or macOS. If it's 'nt', then it indicates Windows.

To obtain detailed system information, you can call the uname() function:

python
print(os.uname())

Note: The uname() function is not available on Windows, meaning some functions in the os module are OS-specific.

Environment Variables

Environment variables defined in the operating system are stored in the os.environ variable, which can be viewed directly:

python
print(os.environ)

To get the value of a specific environment variable, use os.environ.get('key'):

python
print(os.environ.get('PATH'))  # Get the PATH variable
print(os.environ.get('x', 'default'))  # Default value if 'x' doesn't exist

File and Directory Operations

Functions for file and directory operations are divided between the os module and the os.path module. Here's how you can view, create, and delete directories:

  • Get the absolute path of the current directory:

    python
    print(os.path.abspath('.'))
  • Create a new directory:

    python
    new_dir_path = os.path.join('/Users/michael', 'testdir')
    os.mkdir(new_dir_path)  # Create the directory
  • Delete a directory:

    python
    os.rmdir(new_dir_path)  # Remove the directory

When combining paths, avoid directly concatenating strings; use os.path.join() to handle path separators correctly for different operating systems. For example, on Linux/Unix/macOS, it returns:

plaintext
part-1/part-2

On Windows, it returns:

plaintext
part-1\part-2

Similarly, to split paths, use os.path.split():

python
print(os.path.split('/Users/michael/testdir/file.txt'))  # ('/Users/michael/testdir', 'file.txt')

The os.path.splitext() function can be used to get the file extension easily:

python
print(os.path.splitext('/path/to/file.txt'))  # ('/path/to/file', '.txt')

These functions for combining and splitting paths do not require the directories and files to exist; they operate on strings.

File Operations

File operations use the following functions. Assuming there's a test.txt file in the current directory:

  • Rename a file:

    python
    os.rename('test.txt', 'test.py')
  • Delete a file:

    python
    os.remove('test.py')

However, there is no function in the os module to copy files. This is because copying files is not a system call provided by the operating system. Theoretically, we could use file reading and writing to complete file copying, but it would require more code.

Fortunately, the shutil module provides the copyfile() function, along with many other useful functions that supplement the os module.

File Filtering with Python

You can use Python's features to filter files easily. For example, to list all directories in the current directory, you can do this in one line:

python
print([x for x in os.listdir('.') if os.path.isdir(x)])

To list all .py files, you can use:

python
print([x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1] == '.py'])

This approach is very concise!

Summary

The Python os module wraps the operating system's directory and file operations, with some functions found in the os module and others in the os.path module.

Exercises

  1. Write a program using the os module that mimics the output of the dir -l command.
  2. Write a program that searches for files containing a specified string in their names within the current directory and all its subdirectories, and prints their relative paths.
File and Directory Operations has loaded