python 检查输出查找命令不起作用

标签 python find subprocess raspberry-pi2

我试图从插入树莓派的 USB 闪存驱动器中找到名为“config.txt”的文件的路径。使用的物理驱动器可能并不总是相同的,因此路径可能并不总是相同的。所以我用

'find /media/pi/*/config.txt' 

在终端中找到路径并且工作正常。 现在我去使用 check_output 并获得一大串路径。

from subprocess import check_output
cmd = ['find', '/media/pi/*/config.txt']
out = check_output(cmd,shell=True) 

根据 https://docs.python.org/2/library/subprocess.html,我将 shell 设置为 True 以允许通配符

out 的结果是:

'.\n./.Xauthority\n./.xsession-errors\n./Public\n./.dmrc\n./Downloads\n./test.sh\n./.idlerc\n./.idlerc/recent-files.lst\n./.idlerc/breakpoints.lst\n./.asoundrc\n./.bash_logout\n./.profile\n./Templates\n./Music\n./.bash_history\n./Videos\n./.local\n./.local/share\n./.local/share/gvfs-metadata\n./.local/share/gvfs-metadata/home\n./.local/share/gvfs-metadata/home-d6050e94.log\n./.local/share/applications\n./.local/share/recently-used.xbel\n./.local/share/Trash\n.....

它会持续一段时间。 我尝试查看其他几个类似的问题,包括下面的链接,但没有运气。

Store output of subprocess.Popen call in a string

最佳答案

如果您想使用通配符,您需要传递一个字符串,就像您从 shell 中传递的那样:

from subprocess import check_output
cmd = 'find /media/pi/*/config.txt'
out = check_output(cmd,shell=True)

您实际上根本不需要子流程,glob会做你想做的事:

from glob import glob 
files =  glob('/media/pi/*/config.txt')

关于python 检查输出查找命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227420/

相关文章:

file - 使用 AppleScript 在文本文件中查找和替换

javascript - Mongoose 查找具有多个条件的查询

python - 在python中顺序打开和关闭应用程序

Python 子进程给出错误的值(给出 8 位值而不是 64 位值)

subprocess - 如何在 Haskell 中运行使用临时目录的子进程?

python - 如何增加列表中每个项目/元素的值?

python - Django 网站中的 Bokeh 渲染问题

python - Django 将单独的变量序列化为 json

python - 根据现有字典将字典条目的数据帧转换为所有条目的数据帧

Delphi:查找对话框和字符串网格