【发布时间】:2020-12-20 00:10:14
【问题描述】:
我编写了一个程序的三个不同部分(这些部分被命名为 tokenize、countWords 和 printTopMost,它们被放置在一个名为 wordfreq 的文件模块中),它们将组成一个完整的工作程序(我称之为 topmost)。
我希望程序从程序中执行,像这样:
$ python3 topmost.py eng_stopwords.txt examples/article1.txt 20
word 30
words 21
count 11
text 9
000 9
counting 7
fiction 6
novel 6
rules 5
length 5
used 4
usually 4
details 4
software 4
sources 4
processing 4
segmentation 4
rule 4
novels 4
number 3
在程序名称(最上面)之后是两个文件的路径和一个参数。我想通过导入模块 sys 从程序中访问这些参数(或命令行参数),例如:
import sys
然后指定参数,例如:
sys.argv[1]
这是我一直在写的代码:
import wordfreq
import sys
import urllib.request
def main():
words = wordfreq.tokenize(open(sys.argv[1]))
words.close()
frequencies = wordfreq.countWords(words, str(open(sys.argv[2])).strip('\n'))
frequencies.close()
wordfreq.printTopMost(frequencies, sys.argv[3])
# Close the file with the stop words.
main()
当我运行程序时,它会给出结果:
Erics-MBP:Laboration_1 ericjohannesson$ topmost.py eng_stopwords.txt examples/article.txt 20
bash: topmost.py: command not found
我做错了什么?代码中哪里是问题的原因?
如果您需要有关我正在尝试编写的程序或其他任何内容的更多信息,请随时提出。
【问题讨论】:
标签: python file command-line-arguments execute sys