0

我目前正在创建一个程序来从驱动器中提取文件。我想问一下我如何读取使用 python shell 显示的输出?例如:

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)输入的输出读取到另一个命令。这可能吗?

4

1 回答 1

0

我不知道如何捕获 的输出,但您也可以在打印之前print os.path.join(root, file)将调用的输出保存在变量中。os.path.join(root,file)然后你可以使用这个变量来调用你的命令。例如:

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
于 2014-07-02T16:53:55.507 回答