今天爱分享给大家带来java 调用python并传参的方法【附代码】,希望能帮助到大家。
public String executePython(String fromPath, String toPath, String picPath, String fileName) { String success = "500"; String exe = "python"; //要执行python文件的位置 String command = "D:\\pythonProject\\python.py"; //fromPath, toPath, picPath, fileName -->传递给python的参数 String[] cmdArr = new String[]{exe, command, fromPath, toPath, picPath, fileName}; Process process = null; try { process = Runtime.getRuntime().exec(cmdArr); InputStream is = process.getInputStream(); DataInputStream dis = new DataInputStream(is); //视频处理完成 返回1 String result = dis.readLine(); log.info("python返回结果:" + result); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } return success; }
python中获取参数
fromPath = argv[1] toPath = argv[2] dirPath = argv[3] txt_path = argv[4]