-->

Read output from a Python Executed Script

2019-09-06 06:23发布

问题:

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