class argparse.FileType(mode='r', bufsize=-1, encoding=None, errors=None)
The FileType
factory creates objects that can be passed to the type argument of ArgumentParser.add_argument()
. Arguments that have FileType
objects as their type will open command-line arguments as files with the requested modes, buffer sizes, encodings and error handling (see the open()
function for more details):
1 2 3 4 5 | >>> parser = argparse.ArgumentParser() >>> parser.add_argument( '--raw' , type=argparse.FileType( 'wb' , 0)) >>> parser.add_argument( 'out' , type=argparse.FileType( 'w' , encoding= 'UTF-8' )) >>> parser.parse_args([ '--raw' , 'raw.dat' , 'file.txt' ]) Namespace(out=<_io.TextIOWrapper name= 'file.txt' mode= 'w' encoding= 'UTF-8' >, raw=<_io.FileIO name= 'raw.dat' mode= 'wb' >) |
FileType objects understand the pseudo-argument '-'
and automatically convert this into sys.stdin
for readable FileType
objects and sys.stdout
for writable FileType
objects:
1 2 3 4 | >>> parser = argparse.ArgumentParser() >>> parser.add_argument( 'infile' , type=argparse.FileType( 'r' )) >>> parser.parse_args([ '-' ]) Namespace(infile=<_io.TextIOWrapper name= '<stdin>' encoding= 'UTF-8' >) |
New in version 3.4: The encodings and errors keyword arguments.
Please login to continue.