gpt4 book ai didi

python - 无法使用 argparse 运行脚本,--datadir 错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:00 26 4
gpt4 key购买 nike

<分区>

我正在尝试运行已实现 argparse 的脚本。运行它时,出现以下错误:

usage: compute_distances.py [-h] --datadir DATADIR --info INFO --outdir OUTDIR compute_distances.py: error: argument --datadir is required

我试图添加一个 datadir 参数,但是,我想不出有什么方法不会在之后给我一个语法错误...

//更新2:

删除了所有不立即属于 argparse 实现的代码。

#------------------------------------------------------------------------------
# Main program
#------------------------------------------------------------------------------

# Set up the parsing of command-line arguments
parser = argparse.ArgumentParser(description="Compute distance functions on vectors")
parser.add_argument("--datadir", required=True,
help="Path to input directory containing the vectorized data")
parser.add_argument("--info", required=True,
help="Name of file containing information about documents (name and label)")
parser.add_argument("--outdir", required=True,
help="Path to the output directory, where the output file will be created")
args = parser.parse_args()

# Read the info file with details of the documents to process
try:
file_name = "%s/%s" % (args.datadir, args.info)
f_in = open(file_name, 'r')
except IOError:
print "Input file %s does not exist" % file_name
sys.exit(1)

# If the output directory does not exist, then create it
if not os.path.exists(args.outdir):
os.makedirs(args.outdir)

脚本调用

compute_distances.py [-h] "datadir"

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com