为什么可以';我是否读取python中此子流程调用的输出?

为什么可以';我是否读取python中此子流程调用的输出?,python,shell,subprocess,Python,Shell,Subprocess,我想进行子进程调用,以获取名为ORIG的文件夹的目录结构 这是我的密码: import os from subprocess import call # copy the directory structure of ORIG into file f = open("outputFile.txt","r+b") call(['find', './ORIG', '-type', 'd'], stdout=f) a = f.read() print(a) call命令正在工作,因为我打开文件o

我想进行子进程调用,以获取名为ORIG的文件夹的目录结构

这是我的密码:

import os
from subprocess import call
# copy the directory structure of ORIG into file

f = open("outputFile.txt","r+b")
call(['find', './ORIG', '-type', 'd'], stdout=f)

a = f.read()
print(a)
call命令正在工作,因为我打开文件outputFile.txt时看到它的内容:

./ORIG
./ORIG/child_one
./ORIG/child_one/grandchild_one
./ORIG/child_two
但是为什么我不能阅读/打印输出

根据Luke.py的建议,我也尝试了以下方法:

import os
import re
from subprocess import call, PIPE

# copy the directory structure of ORIG into file
# make directory structure from ORIG file in FINAL folder

process = call(['find', './ORIG', '-type', 'd'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()

if stderr:
    print stderr
else:
    print stdout
这给了我答案:

Traceback (most recent call last):
  File "feeder.py", line 9, in <module>
    stdout, stderr = process.communicate()
AttributeError: 'int' object has no attribute 'communicate'
回溯(最近一次呼叫最后一次):
文件“feeder.py”,第9行,在
stdout,stderr=process.communicate()
AttributeError:“int”对象没有属性“communicate”
试试Popen-

您需要从子流程导入管道

process = subprocess.Popen(['find', './ORIG', '-type', 'd'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()

if stderr:
    print stderr
else:
    print stdout
试试波本-

您需要从子流程导入管道

process = subprocess.Popen(['find', './ORIG', '-type', 'd'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()

if stderr:
    print stderr
else:
    print stdout

第一:不需要调用外部程序。如果您想获得某个路径的子目录,那么有一个python函数。您可以使用它并使用
os.path.isdir
检查每个条目,或者使用
os.fwalk
只使用目录

如果您真的想调用外部程序并获取其标准输出,通常高级函数是正确的选择。 您可以通过以下方式获得标准输出:

subprocess.run(command, stdout=subprocess.PIPE).stdout

不需要临时文件或低级函数。

首先:不需要调用外部程序。如果您想获得某个路径的子目录,那么有一个python函数。您可以使用它并使用
os.path.isdir
检查每个条目,或者使用
os.fwalk
只使用目录

如果您真的想调用外部程序并获取其标准输出,通常高级函数是正确的选择。 您可以通过以下方式获得标准输出:

subprocess.run(command, stdout=subprocess.PIPE).stdout

无需临时文件或低级功能。

如果不想在写入和读取之间关闭并重新打开文件,可以使用seek命令从头开始读取文件

import os
from subprocess import call
# copy the directory structure of ORIG into file

f = open("outputFile.txt","r+b")
call(['find', './ORIG', '-type', 'd'], stdout=f)

# move back to the beginning of the file
f.seek(0, 0)

a = f.read()
print(a)

如果不想在写入和读取之间关闭并重新打开文件,可以使用seek命令从头开始读取文件

import os
from subprocess import call
# copy the directory structure of ORIG into file

f = open("outputFile.txt","r+b")
call(['find', './ORIG', '-type', 'd'], stdout=f)

# move back to the beginning of the file
f.seek(0, 0)

a = f.read()
print(a)

我尝试了你的建议,但它给了我一个错误——我更新了我的问题。这就成功了,谢谢。如果你能解释为什么Python无法读取我用原始代码成功编写的txt文件,那也会很有帮助。call返回一个int。不是输出。我尝试了你的建议,但它给了我一个错误——我更新了我的问题。这就成功了,谢谢。如果你能解释为什么Python不能读取我用原始代码成功编写的txt文件,那也会很有帮助。call返回一个int。不是outputGood东西-我只使用了subprocess,因为用户要求的是具体的好东西-我只使用了subprocess,因为用户要求的是具体的