6

I've been looking to incorporate a Python Script a friend made for me into a Java application that I am trying to develop. After some trial and error I finally found out about 'Jython' and used the PythonInterpreter to try and run the script.

However, upon trying to run it, I am getting an error within the Python Script. This is odd because when I try run the script outside of Java (Eclipse IDE in this case), the script works fine and does exactly what I need it to (extract all the images from the .docx files stored in its same directory).

Can someone help me out here?

Java:

import org.python.core.PyException;
import org.python.util.PythonInterpreter;

public class SPImageExtractor
{
    public static void main(String[] args) throws PyException
    {   
        try
        {
            PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
            PythonInterpreter interp = new PythonInterpreter();
            interp.execfile("C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Image-Extractor2.py");
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}

Java Error regarding Python Script:

Traceback (most recent call last):
File "C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Image-Extractor2.py", line 19, in thisDir,_ = path.split(path.abspath(argv[0])) IndexError: index out of range: 0 Traceback (most recent call last):
File "C:/Documents and Settings/user/workspace/Intern Project/Proposals/Converted Proposals/Image-Extractor2.py", line 19, in thisDir,_ = path.split(path.abspath(argv[0])) IndexError: index out of range: 0


Python:

from os import path, chdir, listdir, mkdir, gcwd
from sys import argv
from zipfile import ZipFile
from time import sleep

#A few notes -
#(1) when I do something like " _,variable = something ", that is because
#the function returns two variables, and I only need one.  I don't know if it is a
#common convention to use the '_' symbol as the name for the unused variable, but
#I saw it in some guy's code in the past, and I started using it.
#(2) I use "path.join" because on unix operating systems and windows operating systems
#they use different conventions for paths like '\' vs '/'.  path.join works on all operating
#systems for making paths.

#Defines what extensions to look for within the file (you can add more to this)
IMAGE_FILE_EXTENSIONS = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tif', '.tiff')

#Changes to the directory in which this script is contained
thisDir = getcwd()
chdir(thisDir)

#Lists all the files/folders in the directory
fileList = listdir('.')
for file in fileList:

    #Checks if the item is a file (opposed to being a folder)
    if path.isfile(file):

        #Fetches the files extension and checks if it is .docx
        _,fileExt = path.splitext(file)
        if fileExt == '.docx':

            #Creates directory for the images
            newDirectory = path.join(thisDir, file + "-Images")
            if not path.exists(newDirectory):
                mkdir(newDirectory)

            currentFile = open(file,"r")
            for line in currentFile:
                print line

            sleep(5)



            #Opens the file as if it is a zipfile
            #Then lists the contents
            try:
                zipFileHandle = ZipFile(file)
                nameList = zipFileHandle.namelist()

                for archivedFile in nameList:
                    #Checks if the file extension is in the list defined above
                    #And if it is, it extracts the file
                    _,archiveExt = path.splitext(archivedFile)
                    if archiveExt in IMAGE_FILE_EXTENSIONS:
                        zipFileHandle.extract(archivedFile, newDirectory)
            except:
                pass
This 0ne Pr0grammer
  • 2,632
  • 14
  • 57
  • 81

1 Answers1

2

My guess is that you don't get command line arguments if the interpreter is called (well not that surprisingly, where should it get the correct values? [or what would be the correct value?]).

os.getcwd()

Return a string representing the current working directory.

Would return the working dir, but presumably that's not what you want.

Not tested, but I think os.path.dirname(os.path.realpath( __ file__)) should work presumably (Note: remove the space there; I should look at the formatting options in detail some time~)

Voo
  • 29,040
  • 11
  • 82
  • 156
  • 2
    Wow, that worked great! Thanks a lot man, this place definitely seems way more helpful than all the other forums I've looked into. Lifesaver. – This 0ne Pr0grammer Jun 22 '11 at 19:06
  • 1
    Edit: And that actually is what I wanted as all the files would be stored in the working directory and accessed from there. – This 0ne Pr0grammer Jun 22 '11 at 19:22
  • Okay, this is strange as hell. After I made the necessary changes, everything worked fine yesterday. Now I feel like I'm back to square one because it's still not working and I am not getting any errors. – This 0ne Pr0grammer Jun 23 '11 at 15:31
  • @Joe Well if there are no errors that means whichever solution of the two you're using is specifying the "wrong" directory. I'd first print the result of whatever method you're using and see what it returns. – Voo Jun 24 '11 at 20:48