我正在尝试运行一个具有a解析实现的脚本。在运行它时,我会得到以下错误:
用法: compute_distances.py -h --datadir DATADIR - INFO INFO --outdir OUTDIR compute_distances.py: error:参数--datadir是必需的
我试图添加一个datadir参数,但是,我想不出任何方法不给我一个语法错误之后.
//UPDATE2 2:
删除不立即属于all解析实现的所有代码。
#------------------------------------------------------------------------------
# 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"发布于 2015-09-27 20:30:27
如果您在以下情况下运行脚本:
python compute_distances.py -h在您提供的代码示例中,您将看到argparse模块设置的详细使用说明。这基本上是从每个对parser.add_argument(...)的调用中打印帮助字符串,例如:
parser.add_argument(
"--datadir",
required=True,
help="Path to input directory containing the vectorized data"
)因此,您的脚本调用需要看起来更像:
python compute_distances.py \
--datadir ./my_files/vectorized_data/ \
--info ./my_files/names_and_labels.txt \
--outdir ./my_files/output_data/注意:上面的\只是在每个新行上继续执行这个命令(所以对于堆栈溢出来说,它更易读)--您可以在一行中一起编写它,而不需要\。
https://stackoverflow.com/questions/32812251
复制相似问题