Open compressed file directly with argparse
问题 Can I open a gzip file directly with argparse by changing the type=argparse.FileType() to some gzip type? It's not in the docs, so I'm not sure if argparse even suppoets compressed file types... 回答1: First, the type parameter is a function or other callable that converts a string into something else. That's all. argparse.FileType is factory class that ends up doing something close to: def filetype(astring): mode = 'r' # or 'w' etc. try: f = open(astring, mode) except IOError: raise argparse