首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法运行具有Can解析的脚本--datadir错误

无法运行具有Can解析的脚本--datadir错误
EN

Stack Overflow用户
提问于 2015-09-27 19:58:33
回答 1查看 1K关注 0票数 1

我正在尝试运行一个具有a解析实现的脚本。在运行它时,我会得到以下错误:

用法: compute_distances.py -h --datadir DATADIR - INFO INFO --outdir OUTDIR compute_distances.py: error:参数--datadir是必需的

我试图添加一个datadir参数,但是,我想不出任何方法不给我一个语法错误之后.

//UPDATE2 2:

删除不立即属于all解析实现的所有代码。

代码语言:javascript
复制
#------------------------------------------------------------------------------
# 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)

脚本调用

代码语言:javascript
复制
compute_distances.py [-h] "datadir"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-27 20:30:27

如果您在以下情况下运行脚本:

代码语言:javascript
复制
python compute_distances.py -h

在您提供的代码示例中,您将看到argparse模块设置的详细使用说明。这基本上是从每个对parser.add_argument(...)的调用中打印帮助字符串,例如:

代码语言:javascript
复制
parser.add_argument(
    "--datadir",
    required=True, 
    help="Path to input directory containing the vectorized data"
)

因此,您的脚本调用需要看起来更像:

代码语言:javascript
复制
python compute_distances.py \
    --datadir ./my_files/vectorized_data/ \
    --info ./my_files/names_and_labels.txt \
    --outdir ./my_files/output_data/

注意:上面的\只是在每个新行上继续执行这个命令(所以对于堆栈溢出来说,它更易读)--您可以在一行中一起编写它,而不需要\

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32812251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档