Read output from a Python Executed Script

前提是你 提交于 2019-12-14 04:05:50

问题


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

I would like to read the output of print os.path.join(root,file) input this to another command. Is this possible?


回答1:


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


来源:https://stackoverflow.com/questions/24535769/read-output-from-a-python-executed-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!