python - 用于搜索目录并查找以子字符串结尾的所有字符串的 Python 脚本
问题描述
我正在尝试编写一个 Python 脚本来搜索目录中的所有文件并枚举所有以子字符串“_updated”结尾的字符串。例如,如果我的文件是
// file 1
this is an_updated example file_updated
// file 2
another file here_updated
我希望我的脚本返回:
an_updated
file_updated
here_updated
到目前为止,我编写了以下脚本,用于grep
查找所有包含字符串的文件。我怎样才能修改或扩展它以获得我想要的结果?grep
甚至是正确的方法吗?
#!/bin/bash
files=$(find docs -name "*.txt" -exec grep -l '_updated' {} \;)
解决方案
from os import listdir
dirlist = list(filter(lambda x: x.endswith('_updated', listdir())))
for i in dirlist:
print(i)
这将打印你想要的东西
推荐阅读
- ms-project - MS 项目 REST API
- python - 如何将一系列 IF 语句转换为 Pythonic 代码行?
- visual-studio-code - 如何在 settings.json 中为给定语言指定折叠开始和折叠结束模式
- c# - 匹配音频文件长度的秒和毫秒
- javascript - 为旨在作为集合的对象创建布尔值
- jquery - 如何制作一个过滤类名(多个)下拉列表的 jQuery 过滤器
- javascript - javascript播放声音,好像它来自左侧
- javascript - Javascript - 创建 div 后,Ajax 无法正常工作
- regex - 正则表达式在整个 ini 文件中提取类似的配置块
- javascript - 在js中将逗号分隔符拆分为新行