The command line parser argparse included in the python
standard library allows you to specify a file object as the argument type. it can. (ʻArgparse.FileType`) It's very convenient because you don't have to create a file object from a string.
However, even if the write mode (mode ='w'
) is specified, it will open automatically without saying whether it is present or not, so for a sloppy person, the file is unintentionally overwritten and the contents are erased. There is a risk and it is very dangerous. Therefore, I implemented file overwrite confirmation, so make a note so that you do not forget how to do it.
An example implemented in my work pdf_merge_multipages by referring to the method of the site mentioned at the end of the sentence.
pdf_merge_multipages.py(Excerpt)
class FileTypeWithCheck(argparse.FileType):
def __call__(self, string):
if string and "w" in self._mode:
if os.path.exists(string):
sys.stderr.write(('File: "%s" exists. Is it OK to overwrite? [y/n] : ') % (string))
ans = sys.stdin.readline().rstrip()
ypttrn = re.compile(r'^y(es)?$', re.I)
m = ypttrn.match(ans)
if not m:
sys.stderr.write("Stop file overwriting.\n")
sys.exit(1)
# raise ValueError('Stop file overwriting')
if os.path.dirname(string):
os.makedirs(os.path.dirname(string),
exist_ok=True)
return super(FileTypeWithCheck, self).__call__(string)
def __repr__(self):
return super(FileTypeWithCheck, self).__repr__()
....
....
def main():
argpsr = argparse.ArgumentParser(description='Merge multiple mages in PDF files w/o gap.')
argpsr.add_argument('inputs', metavar='input-file', type=argparse.FileType('rb'),
nargs='+', help='Input PDF file(s)')
argpsr.add_argument('-output', metavar='filename', type=FileTypeWithCheck('wb'),
nargs=1, help='Output file', dest='output', default='a.out.pdf')
...
...
def main():
argpsr = argparse.ArgumentParser(description='Merge multiple mages in PDF files w/o gap.')
argpsr.add_argument('inputs', metavar='input-file', type=argparse.FileType('rb'),
nargs='+', help='Input PDF file(s)')
argpsr.add_argument('-output', metavar='filename', type=FileTypeWithCheck('wb'),
nargs=1, help='Output file', dest='output', default='a.out.pdf')
...
...
if __name__ == '__main__':
main()
If the file exists and you avoid overwriting, you may have to choose between exiting the script (ʻexit () ) or throwing an exception. In the above example, ʻexit ()
is used, but if the file was opened in read mode with the argument of ʻinputs` earlier, the script may end without closing it. The point where the sex remains is a little caught.