Close argument argument argument

argumentparser can take a file type argument and leave the file open directly, for example:

parser.add_argument('infile', nargs='?', type=argparse.FileType('r')) args = parser.parse_args().__dict__ input = args['infile'].readlines() 

Do I need to close args['infile'] in my program? Can an argument close it to me? I did not find mention of this in the docs.

+4
source share
2 answers

NO, it does not close the file type object. see this

The problem is that FileType can return stdin or stdout , so it cannot just close the file object.

A large number of unclosed file descriptors can cause problems for some operating systems, but it is. On the plus side, the fact that argparse accepts any callable for its argument type that can check and convert string input is simple, clean, and works.

Also see this

+1
source

All Articles