0

我是Python中的子流程包的新手。我試圖使用call()方法從包發送下面的命令到終端:Python調用()方法產生java.io.FileNotFoundException

C:\槌-2.0.7 \ BIN \槌進口DIR --input C:\槌-2.0.7 \ inputdirectory --output tutorial.mallet --keep序列--remove-禁用詞

我試圖使用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() 

但是,當我運行上面的代碼時,我ge噸以下錯誤消息:

標籤= C:\槌2.0.7 \ inputdirectory異常在線程 「主」 java.io.FileNotFoundException:C:\槌2.0.7 \ inputdirectory(接入 在 cc.mallet.classify.tui.Text2Vectors在 java.io.FileOutputStream中被拒絕)在java.io.FileOutputStream.open(本機方法)。(來源不明)在 java.io.FileOutputStream中。(來源不明) 。主要(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() 
+0

你爲什麼在已經是絕對路徑的文字上調用'abspath'?你是否期望找回與衆不同的東西?如果是這樣,什麼? – abarnert

+0

同時:頂部顯示的命令行是否在DOS提示符下工作?你爲什麼試圖在'subprocess.call'中生成一個不同於你在DOS提示符下使用的命令行?如果你「打印」命令行而不是「調用」它,然後將它粘貼到DOS提示符中,它是否工作?如果在DOS提示符下輸入C:\ mallet-2.0.7 \ inputdirectory \ foo'然後輸入其中一個文件,會發生什麼情況?你是否從DOS提示符下運行命令的相同工作目錄運行腳本? – abarnert

+0

謝謝@abarnert,這些都是有幫助的問題。 abspath()可能是不必要的;我會削減他們,只是給文字文件路徑。 (我正在使用它們,因爲Windows似乎將文件路徑的某些部分作爲正則表達式在時機上處理)。至於該命令,它可以工作,但前提是它是從C:\ mallet-2.0.7目錄發出的。也就是說,據我所知,我需要在C:\ mallet-2.0.7中,然後我需要發出所需的命令才能工作。 – duhaime

回答

2

你真的沒有給我們足夠的信息可以肯定的,但你的Python代碼是顯然不是在DOS提示符下運行相同的命令行,其中一個差異似乎非常可疑。

想必這個工程:

C:\槌-2.0.7 \ BIN \槌進口DIR --input C:\槌-2.0.7 \ inputdirectory --output tutorial.mallet - 保持序列--remove-禁用詞

但是Python是發電是:

C:\槌-2.0.7 \ BIN \槌進口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文件。

+0

謝謝@abarnert的幫助。我相信我已經修復了輸出參數,並且正在從C:\ mallet-2.0.7目錄運行Python腳本。儘管我沒有收到任何錯誤消息,但我也沒有找到任何輸出。我將在上面發佈我的編輯代碼。如果您發現任何您認爲需要更正的內容,我將非常感謝您提供任何建議。 – duhaime

+0

啊,你已經修復了@abarnert!謝謝! (花了我的時間超過它應該認識到木槌正在意外的目錄中創建輸出。)非常感謝你幫助我解決這個問題! – duhaime

相關問題