从终端中的命令行执行程序,并将路径写入两个文件

问题描述

我编写了程序的三个不同部分(这些部分分别命名为tokenize,countWords和printTopMost,并将它们放置在名为wordfreq的文件模块中),这些部分将组成一个完整的工作程序(我称之为最顶层)。

我希望从程序中执行该程序,如下所示:

$ 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。

您需要在命令行调用中插入min一词,以便您的终端知道您正在使用python来调用python文件。

请注意,我将topmost.py放在python之后,但之前 $

topmost.py