Skip to content

Instantly share code, notes, and snippets.

@martinth
Created August 6, 2015 11:04
Show Gist options
  • Save martinth/ed991fb8cdcac3dfadf7 to your computer and use it in GitHub Desktop.
Save martinth/ed991fb8cdcac3dfadf7 to your computer and use it in GitHub Desktop.
Read from stdin or files in Python (combining argparse and fileinput)
import argpase
import fileinput
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--dummy', help='dummy argument')
parser.add_argument('files', metavar='FILE', nargs='*', help='files to read, if empty, stdin is used')
args = parser.parse_args()
# If you would call fileinput.input() without files it would try to process all arguments.
# We pass '-' as only file when argparse got no files which will cause fileinput to read from stdin
for line in fileinput.input(files=args.files if len(args.files) > 0 else ('-', )):
print(line)
@pinzyte
Copy link

pinzyte commented Dec 31, 2022

In the special case where only 1 file (or none for stdin) is allowed, use: nargs='?', default=[].

The default= is generally required or the default will be None, which will cause fileinput to think it needs to read from sys.argv. This is a problem if you have options on the command line as it will then try to open them as files, and you'll get errors like No such file or directory: '-a'.

Passing an empty array (which is what nargs='*' does when no files are specified) avoids this.

@fish8
Copy link

fish8 commented Aug 10, 2024

Thank you, both this and @drmull's comment were very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment