问题描述
我当前正在创建一个程序来从驱动器中提取文件.我想问一下我如何阅读使用python shell显示的输出?例如:
I am currently creating a program to extract file from drives. I would like to ask how I read an output that is displayed using python shell? For example:
while i<len(drives):
print 'Searching for file in:', drives[i]
print ''
for root, dirs, files in os.walk(drives[i]):
for file in files:
if file.endswith(".vmdk"):
print os.path.join(root, file)
if file.endswith(".vbox"):
print os.path.join(root,file)
i+=1
我想读取print os.path.join(root,file)
的输出,并将其输入到另一个命令.这可能吗?
I would like to read the output of print os.path.join(root,file)
input this to another command. Is this possible?
推荐答案
我不知道如何捕获print os.path.join(root, file)
的输出,但是您也可以将调用的输出保存到os.path.join(root,file)
的变量中,然后印刷.然后,您可以使用此变量来调用您的命令.例如:
I do not know how you can capture the output of print os.path.join(root, file)
, but you could also save the output of the call to os.path.join(root,file)
in a variable before printing. Then you can use this variable to call your command. E.g:
while i<len(drives):
print 'Searching for file in:', drives[i]
print ''
for root, dirs, files in os.walk(drives[i]):
for file in files:
if file.endswith(".vmdk") or file.endswith(".vbox"):
filePath = os.path.join(root, file)
print filePath
// call your command here using 'filePath'
i+=1
这篇关于从Python执行的脚本读取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!