如何 运行 来自 Deno 的 Python 脚本?
How to run a Python Script from Deno?
我有一个 python 脚本,代码如下:
print("Hello Deno")
我想使用 Deno 运行 来自 test.ts 的这个 python 脚本 (test.py)。到目前为止,这是 test.ts 中的代码:
const cmd = Deno.run({cmd: ["python3", "test.py"]});
如何在 Deno 中获取 python 脚本的输出?
Deno.run
returns Deno.Process
的实例。为了得到输出使用.output()
。如果您想阅读内容,请不要忘记传递 stdout/stderr
选项。
// --allow-run
const cmd = Deno.run({
cmd: ["python3", "test.py"],
stdout: "piped",
stderr: "piped"
});
const output = await cmd.output() // "piped" must be set
const outStr = new TextDecoder().decode(output);
const error = await p.stderrOutput();
const errorStr = new TextDecoder().decode(error);
cmd.close(); // Don't forget to close it
console.log(outStr, errorStr);
如果你不通过 stdout
属性 你会直接得到输出到 stdout
const p = Deno.run({
cmd: ["python3", "test.py"]
});
await p.status();
// output to stdout "Hello Deno"
// calling p.output() will result in an Error
p.close()
您也可以将输出发送到文件
// --allow-run --allow-read --allow-write
const filepath = "/tmp/output";
const file = await Deno.open(filepath, {
create: true,
write: true
});
const p = Deno.run({
cmd: ["python3", "test.py"],
stdout: file.rid,
stderr: file.rid // you can use different file for stderr
});
await p.status();
p.close();
file.close();
const fileContents = await Deno.readFile(filepath);
const text = new TextDecoder().decode(fileContents);
console.log(text)
为了检查您需要使用的进程的状态代码.status()
const status = await cmd.status()
// { success: true, code: 0, signal: undefined }
// { success: false, code: number, signal: number }
如果您需要将数据写入 stdin
,您可以这样做:
const p = Deno.run({
cmd: ["python", "-c", "import sys; assert 'foo' == sys.stdin.read();"],
stdin: "piped",
});
// send other value for different status code
const msg = new TextEncoder().encode("foo");
const n = await p.stdin.write(msg);
p.stdin.close()
const status = await p.status();
p.close()
console.log(status)
你需要 运行 Deno with: --allow-run
flag 才能使用 Deno.run
我有一个 python 脚本,代码如下:
print("Hello Deno")
我想使用 Deno 运行 来自 test.ts 的这个 python 脚本 (test.py)。到目前为止,这是 test.ts 中的代码:
const cmd = Deno.run({cmd: ["python3", "test.py"]});
如何在 Deno 中获取 python 脚本的输出?
Deno.run
returns Deno.Process
的实例。为了得到输出使用.output()
。如果您想阅读内容,请不要忘记传递 stdout/stderr
选项。
// --allow-run
const cmd = Deno.run({
cmd: ["python3", "test.py"],
stdout: "piped",
stderr: "piped"
});
const output = await cmd.output() // "piped" must be set
const outStr = new TextDecoder().decode(output);
const error = await p.stderrOutput();
const errorStr = new TextDecoder().decode(error);
cmd.close(); // Don't forget to close it
console.log(outStr, errorStr);
如果你不通过 stdout
属性 你会直接得到输出到 stdout
const p = Deno.run({
cmd: ["python3", "test.py"]
});
await p.status();
// output to stdout "Hello Deno"
// calling p.output() will result in an Error
p.close()
您也可以将输出发送到文件
// --allow-run --allow-read --allow-write
const filepath = "/tmp/output";
const file = await Deno.open(filepath, {
create: true,
write: true
});
const p = Deno.run({
cmd: ["python3", "test.py"],
stdout: file.rid,
stderr: file.rid // you can use different file for stderr
});
await p.status();
p.close();
file.close();
const fileContents = await Deno.readFile(filepath);
const text = new TextDecoder().decode(fileContents);
console.log(text)
为了检查您需要使用的进程的状态代码.status()
const status = await cmd.status()
// { success: true, code: 0, signal: undefined }
// { success: false, code: number, signal: number }
如果您需要将数据写入 stdin
,您可以这样做:
const p = Deno.run({
cmd: ["python", "-c", "import sys; assert 'foo' == sys.stdin.read();"],
stdin: "piped",
});
// send other value for different status code
const msg = new TextEncoder().encode("foo");
const n = await p.stdin.write(msg);
p.stdin.close()
const status = await p.status();
p.close()
console.log(status)
你需要 运行 Deno with: --allow-run
flag 才能使用 Deno.run