Python call()方法生成java.io.FileNotFoundException

2024-04-18 10:48:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉Python中的subprocess包。我正在尝试使用该包中的call()方法将以下命令发送到终端:

C:\mallet-2.0.7\bin\mallet import-dir --input C:\mallet-2.0.7\inputdirectory --output tutorial.mallet --keep-sequence --remove-stopwords

我尝试使用以下Python代码来完成此任务:

import os
from subprocess import call

class Mallet(object):
    def __init__(self, input_path, mallet_path, topics):
        self.mallet_exec = os.path.abspath('C:\\mallet-2.0.7\\bin\\mallet')
        self.input_path = os.path.abspath('C:\\mallet-2.0.7\\inputdirectory')
        self.topics = '14'

    def import_dir(self):
        text_path = self.input_path 
        output = os.path.abspath('C:\\mallet-2.0.7\\inputdirectory')
        call(self.mallet_exec + " import-dir --input " + input_path + " --keep-sequence --output " + output, shell=True)

input_path = os.path.abspath('C:\\mallet-2.0.7\\inputdirectory')
mallet_path = os.path.abspath('C:\\mallet-2.0.7')
output = 'tutorial.mallet'
topics = '14'

malletfunction = Mallet(input_path, mallet_path, topics)
malletfunction.import_dir()

但是,当我运行上述代码时,我收到以下错误消息:

Labels = C:\mallet-2.0.7\inputdirectory Exception in thread "main" java.io.FileNotFoundException: C:\mallet-2.0.7\inputdirectory (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.(Unknown Source) at java.io.FileOutputStream.(Unknown Source) at cc.mallet.classify.tui.Text2Vectors.main(Text2Vectors.java:320)

有人知道我该如何解决这个错误吗?如果别人能在这个问题上有所启发,我将不胜感激。在

(如果可能会有帮助,我在Windows8中使用Python2.7.5)

^{pr2}$

Tags: pathioimportselfinputoutputosdir
1条回答
网友
1楼 · 发布于 2024-04-18 10:48:10

您并没有给我们足够的信息来确定,但是您的Python代码显然运行的命令行与DOS提示符下使用的命令行不同,其中一个差异似乎非常可疑。在

大概这是可行的:

C:\mallet-2.0.7\bin\mallet import-dir input C:\mallet-2.0.7\inputdirectory output tutorial.mallet keep-sequence remove-stopwords

但是Python正在生成的是:

C:\mallet-2.0.7\bin\mallet import-dir input C:\mallet-2.0.7\inputdirectory keep-sequence output C:\mallet-2.0.7\inputdirectory

注意到 output参数中的区别吗?在DOS提示符下,您要求mallet将其输出写入相对路径tutorial.mallet的文件或目录中。在Python中,您要求它将输出写到C:\mallet-2.0.7\inputdirectory。在

假设您没有C:\mallet-2.0.7\inputdirectory的写入权限,或者mallet想写一个文件,而不是一个目录,它不能创建一个名为C:\mallet-2.0.7\inputdirectory的文件,因为那里已经有一个目录。在

相关问题 更多 >