all Python 3deprecated in 3.14argparseterm
argparse.FileType
argument types
Deprecated since 3.14: a type factory that turned an argument into an already opened file. Accept a path instead and open the file after parsing.
Syntax
parser.add_argument('out', type=argparse.FileType('w', encoding='utf-8'))
The file is opened during parsing: if a later argument turns out to be invalid, parse_args() exits and the open file is never closed, and mode 'w' has already truncated it. Use type=pathlib.Path instead and open the file in a with block after parsing. In 3.14 creating a FileType emits PendingDeprecationWarning; no removal version has been set yet.
Examples
import argparse
import pathlib
parser = argparse.ArgumentParser(prog='app')
parser.add_argument('out', type=pathlib.Path) # вместо FileType
args = parser.parse_args(['out.txt'])
with args.out.open('w', encoding='utf-8') as f:
f.write('ok')
print(args.out.read_text(encoding='utf-8')) # → ok
See also
Open in the full glossarydocs.python.orgReport an inaccuracy