bpo-33927: Pass stdin and stdout are default arguments to argpars infile/outfile by 4383 · Pull Request #11992 · python/cpython (original) (raw)

Ok, thanks for the clarification!

There is an issue to use two FileType arguments though, they open the file when parsing command line arguments (https://github.com/python/cpython/blob/master/Lib/argparse.py#L1198).

This means that when infile and outfile refer to the same file, it will get open for writing before you read from it and its content will be lost before the program even begin:

➜  ~ echo foo >test.txt
➜  ~ python3
Python 3.7.2 (default, Jan 13 2019, 12:50:01)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> open('test.txt')
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>
>>> open('test.txt', 'w')
<_io.TextIOWrapper name='test.txt' mode='w' encoding='UTF-8'>
>>>
➜  ~ cat test.txt
➜  ~

I think it's not possible to use two argparse.FileType to solve the bug #33927. If we don't use FileType as the type of the infile argument, making it default to sys.stdout would not make much sense.