How to read a file from command line using Python?

For Python developers, acquiring the ability to extract information from files through the command line is a fundamental skill. Python arms us with potent tools to seamlessly read data from files via the command line, be it for data analysis, information extraction, or file processing. In this article, we embark on a journey through a few distinct methods to achieve file reading through Python's command line interface. Each method bestows its unique functionalities, empowering you to proficiently manage file operations in your Python projects. As a Python coder, you will be guided through each method with clear explanations and real-world code examples. By the time we conclude this article, you shall have mastered the art of data retrieval from files via the command line using Python. Let us set out on this data retrieval expedition with Python!

Understanding Command-Line File Reading

Before start examining and learn from the code examples, let us grasp the concept of command-line file reading in Python. Command-line file reading entails executing Python scripts through the terminal or command prompt, extracting data from files, and performing various operations like data analysis, processing, or generating reports.

The "open()" Function in Action

Our very first example lays the foundation by employing the open() function to read data from a file.


def read_from_file(file_path):

   with open(file_path, 'r') as file:
     data =
   return data

In this demonstration, we introduce the read_from_file() function, having the file_path as its parameter. The open() function then opens the file specified by file_path in read mode ('r'). The with statement ensures the file's automatic closure after reading. We read the entire contents of the file using the read() method, storing it in the data variable, which is then returned to the caller.

Unleashing readline() for Line-by-Line Reading

Our second code example showcases the readline() method, unraveling its prowess in reading lines from a file.


def read_lines_from_file(file_path):
   lines = []
   with open(file_path, 'r') as file:
     line = file.readline()
     while line:
       line = file.readline()
   return lines

In this code scenario, we present the read_lines_from_file() function, taking the file_path as its argument. The open() function grants access to the file specified by file_path in read mode ('r'). The 'with statement', as always, guarantees the file's closure upon reading. A while loop allows for reading each line using the readline() method until none remain. Each line's leading and trailing whitespaces are dutifully stripped using strip(), adding them to the lines list, which is eventually returned to the caller.

Unveiling readlines() to Fetch All Lines

Our next example demonstrates the use of the readlines() method, extracting all lines from the file, and presenting them as a list.


def get_all_lines_from_file(file_path):
   with open(file_path, 'r') as file:
     lines = file.readlines()
   return [line.strip() for line in lines]

In this instance, we introduce the get_all_lines_from_file() function, which accepts file_path as its parameter. The open() function performs its role, granting passage to the file specified by file_path in read mode ('r'). The 'with statement', guarantees the file's closure upon completion. The readlines() method goes on capturing all lines from the file as a list. A list comprehension promptly strips each line of its leading and trailing whitespaces, forging the resulting list, which is then made available to the caller.

Leveraging fileinput to Read Multiple Files

In our final code example, we unleash the power of the fileinput module, to read data from multiple files


import fileinput

def read_multiple_files(file_paths):
   lines = []
   for line in fileinput.input(file_paths):
   return lines

In this final snippet, the read_multiple_files() function accepts a list of file_paths as its argument. The fileinput.input() function goes on accepting a list of filenames, thereby reading data from multiple files. The for loop reads each line from all the files. Before being added to the lines list, each line's leading and trailing whitespaces are dutifully stripped. The result - a list containing all lines from the specified files - is made available to the caller.

Command-line file reading in Python bestows us the power to access and process data efficiently via the terminal or command prompt. Whether employing the open() function for complete file reading, the readline() method for line-by-line extraction, the readlines() method to unveil all lines at once, or the fileinput module for data retrieval from multiple files, each method brings forth unique advantages tailored to your specific data retrieval needs.

As you progress on your Python journey, remember that the ability to read data from files through the command line opens up a realm of possibilities. Practice the skill of data retrieval, and your Python projects will take off with power command-line file reading.

Updated on: 03-Aug-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started