Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

so yesterday I asked a question and I thought I had gotten my answer as everything seemed to work fine. I come back today to try and run my program and find nothing happens, and this time I am not getting any errors at all.

To give you an idea of things, 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 was getting an error within the Python Script. This was resolved by a suggestion a member of SO provided to change the "thisDir = getcwd()", or so I thought. Now I have no clue what could be wrong, 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) when I run it directly from the command line, so I don't know. Any ideas?

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

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

EDIT

Just made some progress, so within Eclipse, when I run the Script by itself (not being called through java) as a "Python Run" everything works fine. However, when I do a "Jython Run" the script spazzes and it will create new directories for the images, however it will not extract the images themselves.

FINAL EDIT

Man, just screw crappy Jython. I just installed python into my path...

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\Python27\\python.exe  \"C:\\Documents and Settings\\user\\workspace\\Intern Project\\Proposals\\Converted Proposals\\ImageExtractor2.py\"");

Bam, worked perfectly.

Hope that helps someone out there.

share|improve this question
1  
I don't know if this is causing you any problems, but you're not importing the getcwd function. Your first line should read: from os import path, chdir, listdir, mkdir, gcwd – Wilduck Jun 23 '11 at 17:02
1  
Haha that was pretty dumb of me, but it still didn't do anything to display an error :( – This 0ne Pr0grammer Jun 23 '11 at 17:37
    
While your use of Process and exec() as shown might be working now, it is not guaranteed to always work. You are supposed to empty the I/O streams attached to the Process whether you need the data or not. In general, you should start a couple of threads that just keep reading the data out of the buffers until the process finishes, even if you do not intend to use the data. – Loduwijk Jun 23 '11 at 18:56
    
@Loduwijk I'm not entirely sure what you mean. Could you provide some sample code? – This 0ne Pr0grammer Jun 23 '11 at 19:45
    
A better example than I could give here is available at this article – Loduwijk Jun 27 '11 at 1:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.