Gui*_*ume 8 python file subdirectory
我正在尝试在python中翻译这个bash行:
find /usr/share/applications/ -name "*.desktop" -exec grep -il "player" {} \; | sort | while IFS=$'\n' read APPLI ; do grep -ilqw "video" "$APPLI" && echo "$APPLI" ; done | while IFS=$'\n' read APPLI ; do grep -iql "nodisplay=true" "$APPLI" || echo "$(basename "${APPLI%.*}")" ; done
Run Code Online (Sandbox Code Playgroud)
结果是显示在Ubuntu系统中安装的所有视频应用程序.
- >读取/ usr/share/applications /目录中的所有.desktop文件
- >过滤字符串"video""player"以查找视频应用程序
- >过滤字符串"nodisplay = true"和"audio"不显示音频播放器和no-gui应用程序
我希望得到的结果是(例如):
kmplayer
smplayer
vlc
xbmc
Run Code Online (Sandbox Code Playgroud)
所以,我试过这段代码:
import os
import fnmatch
apps = []
for root, dirnames, filenames in os.walk('/usr/share/applications/'):
for dirname in dirnames:
for filename in filenames:
with open('/usr/share/applications/' + dirname + "/" + filename, "r") as auto:
a = auto.read(50000)
if "Player" in a or "Video" in a or "video" in a or "player" in a:
if "NoDisplay=true" not in a or "audio" not in a:
print "OK: ", filename
filename = filename.replace(".desktop", "")
apps.append(filename)
print apps
Run Code Online (Sandbox Code Playgroud)
但是我对递归文件有问题......
我该如何解决?谢谢
Mik*_*maa 19
看起来你做错了os.walk()
循环.不需要嵌套的dir循环.
有关正确的示例,请参阅Python手册:
https://docs.python.org/2/library/os.html?highlight=walk#os.walk
for root, dirs, files in os.walk('python/Lib/email'):
for file in files:
with open(os.path.join(root, file), "r") as auto:
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19293 次 |
最近记录: |