(一)目的:實現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