【问题标题】:Implementation of the Linux find command in pythonLinux find命令在python中的实现
【发布时间】:2015-06-22 21:37:12
【问题描述】:

有人可以指出一组关于如何在 Python 脚本中实际实现和使用 find 命令的说明吗?

我看过:https://docs.python.org/2/library/subprocess.html 我不确定如何使用该命令,即使使用subprocess.callsubprocess.Popen 实现得很好。从我读过的关于这个主题的所有 SO 线程来看,这似乎是两个最佳选择。但是,我不确定我需要使用什么样的参数,以及如何在其中具体实现 find 命令。

有人可以演示如何在subprocess.callsubprocess.Popen 的上下文中使用find,以便我以后可以在我的Python 脚本中直接调用find

【问题讨论】:

  • 你试过什么?显示some code。描述什么是错的:你期望发生什么以及会发生什么。

标签: python subprocess


【解决方案1】:

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

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

在上面的示例中,find_output 将包含find 命令stdoutbytes 实例。如果您还想捕获stderr,请将stderr=subprocess.STDOUT 添加为关键字参数。

【讨论】:

    【解决方案2】:

    这个怎么样,

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

    【讨论】:

      猜你喜欢
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2021-12-05
      • 2012-06-09
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多