0
我试图通过使用stdout的文件对象来实验subprocess.Popen()构造函数。当我运行下面的代码时,文件被创建(包含内容),但内容不显示在控制台上。我无法理解我缺少的东西。任何人都可以指出我的错误(如果有的话)?无法从Python中的'stdout = file_object'的内容读取文件
file_name = "abc.txt"
fo = open(file_name, 'w')
cmd = 'dir'
child = subprocess.Popen(cmd, stdout=fo, stderr=fo, shell=True)
fo.close()
with open(file_name, 'r') as fo: print(fo.read())