使用模式将多个文本文件作为参数传递给脚本

Passing multiple text files as arguments to a script using a pattern

首先,我想声明这是一个练习的调试问题,但我无法从讲师那里得到任何帮助,尽管我已经阅读了很多论据,但我可以'似乎想不通,所以我来了。所以我有一个 python 脚本来比较作为参数传递的 .txt 文件。目前是这样称呼它的:

python compare.py -s stop_list.txt NEWS/news01.txt NEWS/news02.txt

并且使用

将文件解析为名称列表
import sys, re, getopt, glob

opts, args = getopt.getopt(sys.argv[1:],'hs:bI:')
opts = dict(opts)
filenames = args

if '-I' in opts:
    filenames = glob.glob(opts['-I'])

print('INPUT-FILES:', ' '.join(filenames))
print(filenames)

我可以传递两个以上的文件,只需将它们一起列出即可

python compare.py -s stop_list.txt NEWS/news01.txt NEWS/news02.txt NEWS/news03.txt NEWS/news04.txt

但这很快就会变得不切实际。

现在建议可以使用模式传递更多文件

python compare.py -s stop_list.txt -I ’NEWS/news??.txt’
i.e.:
python compare.py -s stop_list.txt -I ’NEWS/news0[123].txt’

不过它的行为似乎有点奇怪。首先,如果我写:

python compare.py -s stop_list.txt -I NEWS/news01.txt NEWS/news02.txt

只有 news01.txt 会传递给脚本。

接下来,当按照建议使用模式时,没有任何输入。我无法真正理解是解析输入文件的代码有误需要修改,还是我做错了什么。

-h 状态:

USE: python <PROGNAME> (options) file1...fileN
OPTIONS:
    -h : print this help message
    -b : use BINARY weights (default: count weighting)
    -s FILE : use stoplist file FILE
    -I PATT : identify input files using pattern PATT, 
              (otherwise uses files listed on command line)

提前致谢:)

检查报价。他们看起来很特别。请尝试使用 ' 或 "。