How to take input file from terminal for python script?

Learn how to take input file from terminal for python script? with practical examples, diagrams, and best practices. Covers python development techniques with visual explanations.

How to Take Input File from Terminal for Python Scripts

Hero image for How to take input file from terminal for python script?

Learn various methods to pass file paths and content to your Python scripts directly from the command line, enhancing script flexibility and automation.

When developing Python scripts, you often need to process data from external files. Instead of hardcoding file paths within your script, it's best practice to allow users to specify input files directly from the terminal. This makes your scripts more versatile, reusable, and easier to integrate into larger workflows. This article explores several common and effective methods for handling file input from the command line in Python.

Method 1: Command-Line Arguments with sys.argv

The sys module provides access to system-specific parameters and functions, including sys.argv, which is a list of command-line arguments passed to a Python script. The first element, sys.argv[0], is always the script's name itself. Subsequent elements (sys.argv[1], sys.argv[2], etc.) are the arguments provided by the user.

import sys

def process_file_argv(filepath):
    try:
        with open(filepath, 'r') as f:
            content = f.read()
            print(f"Content from '{filepath}':\n{content[:100]}...") # Print first 100 chars
    except FileNotFoundError:
        print(f"Error: File '{filepath}' not found.")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    if len(sys.argv) > 1:
        input_file = sys.argv[1]
        process_file_argv(input_file)
    else:
        print("Usage: python your_script.py <filepath>")

Example using sys.argv to get a file path from the command line.

Method 2: Advanced Argument Parsing with argparse

The argparse module is the recommended way to parse command-line arguments in Python. It provides a powerful and flexible way to define arguments, generate help messages, and handle various argument types (positional, optional, flags). This is ideal for scripts that require more than just a single file path.

import argparse

def process_file_argparse(filepath, mode='r'):
    try:
        with open(filepath, mode) as f:
            content = f.read()
            print(f"Content from '{filepath}' (mode: {mode}):\n{content[:100]}...")
    except FileNotFoundError:
        print(f"Error: File '{filepath}' not found.")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Process a text file.')
    parser.add_argument('file', type=str, help='The path to the input file.')
    parser.add_argument('-m', '--mode', type=str, default='r', help='File open mode (e.g., r, w, a). Default is r.')

    args = parser.parse_args()
    process_file_argparse(args.file, args.mode)

Using argparse to define a required file argument and an optional mode argument.

Method 3: Piping Input with sys.stdin

Sometimes, you don't want to pass a file path, but rather the content of a file directly as standard input (stdin). This is common in Unix-like environments where you can pipe the output of one command as the input to another. Python's sys.stdin allows your script to read this piped input.

import sys

def process_stdin():
    print("Reading from stdin...")
    content = sys.stdin.read()
    if content:
        print(f"Received content via stdin:\n{content[:100]}...")
    else:
        print("No content received via stdin.")

if __name__ == '__main__':
    process_stdin()

Reading file content piped directly into the script via sys.stdin.

flowchart TD
    A["User executes command"] --> B["cat input.txt"];
    B --> C["Pipe operator (|)"];
    C --> D["python script.py"];
    D --> E["script.py reads sys.stdin"];
    E --> F["Processes file content"];

Workflow for piping file content into a Python script using sys.stdin.

Choosing the Right Method

The best method depends on your script's complexity and how you expect users to interact with it:

  • sys.argv: Best for very simple scripts requiring one or two positional arguments, where a full-fledged argument parser is overkill.
  • argparse: Ideal for most production-ready scripts that need robust argument handling, optional flags, type checking, and automatic help generation.
  • sys.stdin: Perfect for scripts designed to be part of a larger command-line pipeline, where the input is streamed rather than read from a named file.