无法在命令行中将 python 脚本的标准输出存储为字符串
Cannot store std output of python script in command line as string
难道我不能简单地在我的命令行中输入 "python" 并检索吐出的文本吗?
try {
Process proc = Runtime.getRuntime().exec("python");
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String result = null;
while ((result = reader.readLine()) != null) {
PrintWriter out = resp.getWriter();
out.println("<div>" + result + "</div>");
}
} catch (IOException e) {
e.printStackTrace();
}
期望的结果是:
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
运行 python C:\Users\Me\HelloWorld.py
执行脚本并收集标准输出:
try {
Process proc = Runtime.getRuntime().exec("python C:\Users\Me\HelloWorld.py");
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String result = null;
while ((result = reader.readLine()) != null) {
PrintWriter out = resp.getWriter();
out.println("<div>" + result + "</div>");
}
} catch (IOException e) {
e.printStackTrace();
}
难道我不能简单地在我的命令行中输入 "python" 并检索吐出的文本吗?
try {
Process proc = Runtime.getRuntime().exec("python");
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String result = null;
while ((result = reader.readLine()) != null) {
PrintWriter out = resp.getWriter();
out.println("<div>" + result + "</div>");
}
} catch (IOException e) {
e.printStackTrace();
}
期望的结果是:
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
运行 python C:\Users\Me\HelloWorld.py
执行脚本并收集标准输出:
try {
Process proc = Runtime.getRuntime().exec("python C:\Users\Me\HelloWorld.py");
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String result = null;
while ((result = reader.readLine()) != null) {
PrintWriter out = resp.getWriter();
out.println("<div>" + result + "</div>");
}
} catch (IOException e) {
e.printStackTrace();
}