A lot of people use Python as a replacement for shell scripts, using it to automate common system tasks, such as manipulating files, configuring systems, and so forth. This article aims to describe accepting Script Input via Redirection, Pipes, or Input Files.
Problem – To have a script to be able to accept input using whatever mechanism is easiest for the user. This should include piping output from a command to the script, redirecting a file into the script, or just passing a filename, or list of filenames, to the script on the command line.
Python’s built-in fileinput module makes this very simple and concise if the script looks like this.
Code #1 :
import fileinput with fileinput.input() as f_input: for line in f_input: print(line, end ='') |
Then input to the script can already be accepted in all of the previously mentioned ways. If the script is saved and make it executable then the one can get the expected output using all of the following:
Code #2 :
# Prints a directory listing to stdout. $ ls | ./filein.py # Reads/etc/passwd to stdout. $ ./filein.py/etc/passwd # Reads/etc/passwd to stdout. $ ./filein.py < /etc/passwd |
The fileinput.input() function creates and returns an instance of the FileInput class. In addition to containing a few handy helper methods, the instance can also be used as a context manager. So, to put all of this together, if one wrote a script that expected to be printing output from several files at once, one might have it include the filename and line number in the output, as shown in the code given below –
Code #3 :
import fileinput with fileinput.input('/etc/passwd') as f: for line in f: print(f.filename(), f.lineno(), line, end ='') |
/etc/passwd1 /etc/passwd2 /etc/passwd3 <other output omitted>
Using it as a context manager ensures that the file is closed when it’s no longer being used, and one leveraged a few handy FileInput helper methods here to get some extra information in the output.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- PyQt5 QColorDialog - Accepting the color
- Python | Add Logging to a Python Script
- Python script to open a Google Map location on clipboard
- Run Python script from Node.js using child process spawn() method
- Autorun a Python script on windows startup
- Python Script to Shutdown Computer
- Python Script to Restart Computer
- Python Script to Logout Computer
- Run python script from anywhere in linux
- Get directory of current Python script
- How to Run a Python Script
- Schedule a Python Script on PythonAnywhere
- Convert Python Script to .exe File
- Add a User in Linux using Python Script
- Deleting a User in Linux using Python Script
- Get Your System Information - Using Python Script
- Python script to change MAC address of Linux machine
- Run Python Script using PythonShell from Node.js
- Create a Python Script Notifying to take a break
- Python script to get device vendor name from MAC Address
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

