I have a CLI script and want it to read data from a file. It should be able to read it in two ways :
cat data.txt | ./my_script.py
./my_script.py data.txt
—a bit like grep
, for example.
What I know:
sys.argv
and optparse
let me read any args and options easily.sys.stdin
let me read data piped infileinput
make the full process automaticUnfortunately:
fileinput
uses stdin and any args as input. So I can't use options that are not filenames as it tries to open them.sys.stdin.readlines()
works fine, but if I don't pipe any data, it hangs until I enter Ctrl + D
stdin
is always True
in a boolean context.I'd like a portable way to do this if possible.
We can use the fileinput module to read from stdin in Python. fileinput. input() reads through all the lines in the input file names specified in command-line arguments. If no argument is specified, it will read the standard input provided.
Short for standard input, stdin is an input stream where data is sent to and read by a program. It is a file descriptor in Unix-like operating systems, and programming languages, such as C, Perl, and Java.
Argparse allows this to be done in a fairly easy manner, and you really should be using it instead of optparse
unless you have compatibility issues.
The code would go something like this:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--input', type = argparse.FileType('r'), default = '-')
Now you have a parser that will parse your command line arguments, use a file if it sees one, or use standard input if it doesn't.
Process your non-filename arguments however you'd like, so you wind up with an array of non-option arguments, then pass that array as the parameter to fileinput.input()
:
import fileinput
for line in fileinput.input(remaining_args):
process(line)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With