duhaime
duhaime

Reputation: 27611

Python call() method yields java.io.FileNotFoundException

I am new to the subprocess package in Python. I am trying to use the call() method from that package to send the following command to the terminal:

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

I have attempted to use the following bit of Python code to accomplish this task:

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()

When I run the above code, however, I get the following error message:

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)

Does anyone know how I might resolve this error? I would be most grateful for any light others can shed on this question.

(In case it might help, I'm working in Windows 8, with 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()

Upvotes: 0

Views: 923

Answers (1)

abarnert
abarnert

Reputation: 366083

You haven't really given us enough information to be sure, but your Python code is clearly not running the same command line you're using at the DOS prompt, and one of the differences seems very suspicious.

Presumably this works:

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

But what Python is generating is:

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

Notice the difference in the --output parameter? In the DOS prompt, you're asking mallet to write its output to a file or directory at the relative path tutorial.mallet. In Python, you're asking it to write its output to C:\mallet-2.0.7\inputdirectory.

Presumably, either you don't have write permission to C:\mallet-2.0.7\inputdirectory, or mallet wants to write a file, not a directory, and it can't create a file named C:\mallet-2.0.7\inputdirectory because there's already a directory there.

Upvotes: 2

Related Questions