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

时间:2013-06-25 20:27:10

标签: python python-2.7 command-line subprocess command-line-arguments

我是Python中的子进程包的新手。我试图使用该包中的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()

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

  

标签= C:\ mallet-2.0.7 \ inputdirectory线程“main”中的异常   java.io.FileNotFoundException:C:\ mallet-2.0.7 \ inputdirectory(Access   被拒绝)在java.io.FileOutputStream.open(Native Method)at   java.io.FileOutputStream。(未知来源)at   java.io.FileOutputStream。(未知来源)at   cc.mallet.classify.tui.Text2Vectors.main(Text2Vectors.java:320)

有谁知道如何解决此错误?我会非常感谢其他人可以解决这个问题。

(如果它可能有帮助,我在Windows 8中使用Python 2.7.5)

################
# EDITED CODE: #
################

import os
from subprocess import call

class Mallet(object):
    def __init__(self, input_path, mallet_path = 'C:\\mallet-2.0.7'):
        self.mallet_exec = mallet_path + "\\bin\\mallet"
        self.input_path = 'C:\\mallet-2.0.7\\inputdirectory'

    def import_dir(self):
        text_path = self.input_path 
        output = "preparedforinput.mallet"
        call(self.mallet_exec + " import-dir --input " + input_path + " --keep-sequence --output " + output , shell=True)

input_path = 'C:\\mallet-2.0.7\\inputdirectory'
mallet_path = 'C:\\mallet-2.0.7'

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

1 个答案:

答案 0 :(得分:2)

您还没有给我们足够的信息以确定,但您的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 < / p>

注意--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的文件因为那里已有一个目录。