I have a CLI script and want him to read data from a file. It should be able to read it in two ways :
cat data.txt | ./my_script.py
A bit like grep, for example.
What I know:
optparse let me read any args and options easily.
sys.stdin let me read data piped in
fileinput make the full process automatic
fileinput use 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() work fine, but it I don't pipe any data, it hangs until I enter Ctrl + D
I don't know how to implement "if nothing in stdin, read from a file in args" because stdin is always True in a boolean context.
I'd like a portable way to do so is possible.
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 )
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.
There is no reliable way to detect if
sys.stdin is connected to anything, nor is it appropriate do so (e.g., the user wants to paste the data in). Detect the presence of a filename as an argument, and use stdin if none is found.
For unix/linux you can detect whether data is being piped in by looking at
$ date | python - c "import os;print os.isatty(0)" False $ python - c "import os;print os.isatty(0)" True
I'm not sure there is an equivalent for Windows.
Ok, I tried it with python2.6 on windows XP
C :\ Python26 > echo "hello" | python . exe - c "import os;print os.isatty(0)" False C :\ Python26 > python . exe - c "import os;print os.isatty(0)" True
So maybe it it not all hopeless for windows
ScrapBook :: Firefox Extension
订阅 substack 体验古早写作:
点击注册~> 获得 100$ 体验券:
关于 ~ DebugUself with DAMA ;-)