我是 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()
但是,当我运行上述代码时,我收到以下错误消息:
标签 = C:\mallet-2.0.7\inputdirectory 线程“main”中的异常 java.io.FileNotFoundException: C:\mallet-2.0.7\inputdirectory (Access is denied) at java.io.FileOutputStream.open(Native Method ) 在 java.io.FileOutputStream.(Unknown Source) at java.io.FileOutputStream.(Unknown Source) at cc.mallet.classify.tui.Text2Vectors.main(Text2Vectors.java:320)
有谁知道我如何解决这个错误?如果其他人能对这个问题有所了解,我将不胜感激。
(如果它可能有帮助,我正在使用 Python 2.7.5 在 Windows 8 中工作)
################
# 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()