@@ -865,16 +865,14 @@ See also :ref:`specifying-ambiguous-arguments`. The supported values are:
865865 output files::
866866
867867 >>> parser = argparse.ArgumentParser()
868- >>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
869- ... default=sys.stdin)
870- >>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
871- ... default=sys.stdout)
868+ >>> parser.add_argument('infile', nargs='?')
869+ >>> parser.add_argument('outfile', nargs='?')
872870 >>> parser.parse_args(['input.txt', 'output.txt'])
873- Namespace(infile=<_io.TextIOWrapper name='input.txt' encoding='UTF-8'>,
874- outfile=<_io.TextIOWrapper name='output.txt' encoding='UTF-8'>)
871+ Namespace(infile='input.txt', outfile='output.txt')
872+ >>> parser.parse_args(['input.txt'])
873+ Namespace(infile='input.txt', outfile=None)
875874 >>> parser.parse_args([])
876- Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>,
877- outfile=<_io.TextIOWrapper name='<stdout>' encoding='UTF-8'>)
875+ Namespace(infile=None, outfile=None)
878876
879877.. index :: single: * (asterisk); in argparse module
880878
@@ -1033,7 +1031,6 @@ Common built-in types and functions can be used as type converters:
10331031 parser.add_argument('distance', type=float)
10341032 parser.add_argument('street', type=ascii)
10351033 parser.add_argument('code_point', type=ord)
1036- parser.add_argument('dest_file', type=argparse.FileType('w', encoding='latin-1'))
10371034 parser.add_argument('datapath', type=pathlib.Path)
10381035
10391036User defined functions can be used as well:
@@ -1827,9 +1824,19 @@ FileType objects
18271824 >>> parser.parse_args(['-'])
18281825 Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>)
18291826
1827+ .. note ::
1828+
1829+ If one argument uses *FileType * and then a subsequent argument fails,
1830+ an error is reported but the file is not automatically closed.
1831+ This can also clobber the output files.
1832+ In this case, it would be better to wait until after the parser has
1833+ run and then use the :keyword: `with `-statement to manage the files.
1834+
18301835 .. versionchanged :: 3.4
18311836 Added the *encodings * and *errors * parameters.
18321837
1838+ .. deprecated :: 3.14
1839+
18331840
18341841Argument groups
18351842^^^^^^^^^^^^^^^
0 commit comments