(一)目的:实现web后台调取python脚本
(二)实现:
1、方案1使用Runtime.getRuntime().exec(command)函数
(1)在控制映射处写Runtime.getRuntime().exec(command)函数
@RequestMapping("/openLight") public ModelAndView openLight() throws IOException { ModelAndView mav = new ModelAndView("openLight"); String command = "cmd.exe /c cd" + " E:\\projects\\python\\smarthome\\venv\\Include " + "&& start python main.py"; Process p = Runtime.getRuntime().exec(command); return mav; }
(2)该文件是运行E:\\projects\\python\\smarthome\\venv\\Include 下的main.py以此实现简单的调用
if __name__ == '__main__': fp = open("test.txt", 'w')
注意:
在window下用\表示路径,而在linux都是用/表示路径。在有路径需要修改的时候,要注意区分。
更多方法请参考:https://blog.csdn.net/qq_26591517/article/details/80441540