I seem to have stumbled onto a clear and concise way to close argparse
files:
parser = argparse.ArgumentParser(description='Hello!')
parser.add_argument('src_file',
type=argparse.FileType('r', encoding='Windows-1252'), help='Input .txt file')
parser.add_argument('dst_file',
type=argparse.FileType('w', encoding='Windows-1252'), help='Output .foo file')
args = parser.parse_args()
# do your thing...
args.src_file.close()
args.dst_file.close()
#close(args.src_file) don't do this... gives error
This is with Python 3.10.4; googling the PEPs for this 'feature', nothing was found.
So either this:
- Has existed all along but nobody knew,
- Was added at some point but not mentioned,
- Or it doesn't actually close the file (and doesn't give any error.)*
*The Python debugger in Code/Win10 reports the <_io.TextIOWrapper>
's closed
boolean does change to True
from this .close()
, so it seems to be working as expected.