有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java(Eclipse)中使用Python脚本

我一直在寻找将一位朋友为我制作的Python脚本合并到我正在尝试开发的Java应用程序中。经过一番尝试和错误,我终于发现了关于“Jython”的内容,并使用PythonInterpreter尝试运行脚本

但是,在尝试运行它时,Python脚本中出现了一个错误。这很奇怪,因为当我尝试在Java之外运行脚本时(在本例中为Eclipse IDE),该脚本工作正常,并且完全按照我的需要执行(从存储在同一目录中的.docx文件中提取所有图像)

有人能帮我吗

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脚本的Java错误:

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

共 (1) 个答案

  1. # 1 楼答案

    我的猜测是,如果调用解释器,就不会得到命令行参数(这并不奇怪,它应该从哪里获得正确的值?[或者正确的值是什么?])

    os.getcwd()

    Return a string representing the current working directory.
    

    将返回工作目录,但可能这不是您想要的

    没有经过测试,但我认为是操作系统。路径dirname(os.path.realpath(_uuu文件__u))应该可以工作(注意:删除那里的空格;我应该在某个时候详细查看格式化选项~)