2015-06-22 133 views
0

有人能指出我有关如何在Python脚本中实际实现和使用find命令的一组指令吗?在Python中执行Linux find命令

我已经看过:https://docs.python.org/2/library/subprocess.html 即使执行顺利,subprocess.callsubprocess.Popen,我也不确定如何使用该命令。从我已阅读的所有关于此主题的SO线索看来,这似乎是两个最佳选择。但是,我不确定我需要使用哪种参数,以及如何在其中专门实施find命令。

可有人请演示如何使用findsubprocess.callsubprocess.Popen的背景下,这样我以后可以在我的Python脚本直接调用find

+0

你尝试过什么?显示[一些代码](http://stackoverflow.com/help/mcve)。描述什么是错的:你期望发生什么,取而代之的是什么。 – jfs

回答

0

您可能想要使用check_output()辅助函数。

find_output = subprocess.check_output('find ~', shell = True) 

在上述例子中,find_output将包含find命令stdoutbytes实例。如果您还希望捕获stderr,请将stderr=subprocess.STDOUT作为关键字参数添加。

0

这个怎么样,

found = subprocess.Popen(['find', '.'],stdout=subprocess.PIPE) 
for line in iter(found.stdout.readline, ''): 
    print line,