Using the type
parameter of the argparse.add_argument
method, you can require an argument to be a readable file:
parser.add_argument('--sqlite-file', type=argparse.FileType('r'))
As a benefit of specifying this type, argparse checks whether the file can be read and displays an error to the user if not.
Is there a way to obtain the passed filename instead of an instance of io.TextIOWrapper
or io.BufferedReader
?
Since the filename appears in the string representation of the parser ('sqlite_file': <_io.TextIOWrapper name='data/export.sqlite' ...
, or 'sqlite_file': <_io.BufferedReader name='data/export.sqlite' ...>
) it should be possible.
How to do it?