文件作为argparse的命令行参数 - 如果参数无效,则出现错误消息

Mar*_*oma 53 python argparse

我目前正在使用这样的argparse:

import argparse
from argparse import ArgumentParser

parser = ArgumentParser(description="ikjMatrix multiplication")
parser.add_argument("-i", dest="filename", required=True,
    help="input file with two matrices", metavar="FILE")
args = parser.parse_args()

A, B = read(args.filename)
C = ikjMatrixProduct(A, B)
printMatrix(C)
Run Code Online (Sandbox Code Playgroud)

现在我想指出,该参数-i应该是一个可读的文件.我怎样才能做到这一点?

我已经尝试添加type=open,type=argparse.FileType('r')但他们工作,但如果文件无效,我想收到一条错误消息.我怎样才能做到这一点?

mgi*_*son 77

实际上这很容易.您只需编写一个函数来检查文件是否有效,否则写入错误.将该功能与该type选项一起使用.请注意,您可以通过子类化获得更多花哨并创建自定义操作argparse.Action,但我认为这不是必需的.在我的示例中,我返回一个打开的文件句柄(见下文):

#!/usr/bin/env python

from argparse import ArgumentParser
import os.path


def is_valid_file(parser, arg):
    if not os.path.exists(arg):
        parser.error("The file %s does not exist!" % arg)
    else:
        return open(arg, 'r')  # return an open file handle


parser = ArgumentParser(description="ikjMatrix multiplication")
parser.add_argument("-i", dest="filename", required=True,
                    help="input file with two matrices", metavar="FILE",
                    type=lambda x: is_valid_file(parser, x))
args = parser.parse_args()

A, B = read(args.filename)
C = ikjMatrixProduct(A, B)
printMatrix(C)
Run Code Online (Sandbox Code Playgroud)

  • 实际上,尝试使用try-except块打开文件比检查是否存在更好 (10认同)
  • @jarondl是对的.这应该改为使用`try:...除了IOError`以避免潜在的竞争条件.对于大多数情况来说,这无关紧要,但这最近让我感到困惑. (3认同)
  • @moose——另一条评论。`os.path.isfile` 可能比 `os.path.exists` 更合适(取决于你是否也想接受目录) (2认同)

mko*_*bit 32

在Python 3.4中执行此操作的方法是使用argparse.FileType该类.确保在完成后关闭输入流.这也很有用,因为您可以'-'对STDIN/STDOUT 使用伪参数.从文档:

FileType对象理解伪参数'-'并自动将其转换sys.stdin为可读FileType对象和sys.stdout可写FileType对象

例:

#!/usr/bin/env python3

import argparse

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--infile', type=argparse.FileType('r', encoding='UTF-8'), 
                      required=True)
  args = parser.parse_args()
  print(args)
  args.infile.close()
Run Code Online (Sandbox Code Playgroud)

跑的话......

  • 没有争论:

    $ ./stack_overflow.py
    usage: stack_overflow.py [-h] --infile INFILE
    stack_overflow.py: error: the following arguments are required: --infile
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用不存在的文件:

    $ ./stack_overflow.py --infile notme
    usage: stack_overflow.py [-h] --infile INFILE
    stack_overflow.py: error: argument --infile: can't open 'notme': [Errno 2] No such file or directory: 'notme'
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用现有文件:

    $ ./stack_overflow.py --infile ./stack_overflow.py
    Namespace(infile=<_io.TextIOWrapper name='./stack_overflow.py' mode='r' encoding='UTF-8'>)
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用'-'的标准输入:

    $ echo 'hello' | ./stack_overflow.py --infile -
    Namespace(infile=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>)
    
    Run Code Online (Sandbox Code Playgroud)

  • `argparse.FileType`也可以在Python 2.7中使用. (8认同)

Mar*_*oma 17

我刚发现这个:

def extant_file(x):
    """
    'Type' for argparse - checks that file exists but does not open.
    """
    if not os.path.exists(x):
        # Argparse uses the ArgumentTypeError to give a rejection message like:
        # error: argument input: x does not exist
        raise argparse.ArgumentTypeError("{0} does not exist".format(x))
    return x

if __name__ == "__main__":
    import argparse, sys, os
    from argparse import ArgumentParser

    parser = ArgumentParser(description="ikjMatrix multiplication")
    parser.add_argument("-i", "--input",
        dest="filename", required=True, type=extant_file,
        help="input file with two matrices", metavar="FILE")
    args = parser.parse_args()

    A, B = read(args.filename)
    C = ikjMatrixProduct(A, B)
    printMatrix(C, args.output)
Run Code Online (Sandbox Code Playgroud)

资料来源:fhcrc.github.com


lel*_*uch 5

在 python3.8+ 上使用 argparse。这将返回 Pathlib.Path。

import argparse
from pathlib import Path

def validate_file(arg):
    if (file := Path(arg)).is_file():
        return file
    else:
        raise FileNotFoundError(arg)

parser = argparse.ArgumentParser()
parser.add_argument(
    "--file", type=validate_file, help="Input file path", required=True
)
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)