Python is known for many things, and one of them is its ability to function as a scripting language. It’s a great language to glue things together and interact with the underlying operating system. This section shows you how to work with files, run other programs, and read settings and arguments from the environment your script runs in.
Files and directories
Most scripts read or write files at some point. The article on Python files covers opening, reading, writing and appending, plus everyday operations like checking if a file exists, creating directories, and copying, moving and deleting files with the os and shutil modules. If you only need to read a text file, How to read a text file in Python gets you there quickly.
For working with paths, modern Python code uses the pathlib module. A Path object knows how to join paths, find files and read their properties, and it works the same on Windows, macOS and Linux. For example, this lists all text files in the current directory with their size:
from pathlib import Path
for path in Path(".").glob("*.txt"):
print(path.name, path.stat().st_size, "bytes")Code language: Python (python)
Running other programs
Python can start other programs, wait for them to finish, and capture what they print. The subprocess article shows how to do this with subprocess.run, how to feed a program input, and how to avoid the security problems that come with running shell commands.
Environment variables and command-line arguments
Your script can also read information from the outside world. Environment variables live in os.environ, which works like a dictionary. The arguments you typed after the script name end up in sys.argv:
import os
import sys
mode = os.environ.get("APP_MODE", "development")
print("Mode:", mode)
print("Arguments:", sys.argv[1:])Code language: Python (python)
If you save this as env.py and run it on macOS or Linux like this, you get:
The first item in sys.argv is the script name, which is why the example skips it. Once your script takes more than one or two arguments, use the argparse module. It parses options like --verbose for you and generates a --help message.
Related: the Unix shell
If you write scripts on Linux or macOS, it helps to know your way around the command line too. The Unix shell section covers the basic commands, pipes and Bash scripts.