【发布时间】:2012-07-03 14:03:48
【问题描述】:
我正在运行一个 python 程序:
import os
os.system("ls") # ls command runs on the terminal
将输出存储在文件中:
os.system("ls > a.txt")
我需要的是,它将输出存储在一些临时字符串中。这可能吗??
【问题讨论】:
-
没有。你不能那样做。系统调用后将a.txt的内容读入变量。最好使用 python 管道。
-
@SridharJagannathan 你错了。当然你可以这样做,使用 python 的 subprocess 模块。
-
@BigYellowCactus 我确实提到了管道。
标签: python unix system-calls