python-3.x - 替换输出长度不同的控制台输出
问题描述
我有一个程序可以搜索给定存储库中的文件以检查它们是否包含字符串...
print("Searching files for string...")
for folder, dirs, files in os.walk(some_path):
for file in files:
if file.endswith(".st") or file.endswith(".jsp"):
fullpath = os.path.join(folder, file)
# print fullpath
print(fullpath, end="\r", flush=True)
time.sleep(0.5)
# end print fullpath
with open(fullpath, 'r') as read_file:
for (line_number, line) in enumerate(read_file):
if search_term in line:
results_array.append({
"filepath": fullpath,
"line": line_number + 1
})
我想将完整路径打印到控制台但替换该行,而不是在循环迭代时控制台变得充满输出。使用当前代码,经过几次迭代,输出开始与前几行合并,我假设这是因为每次迭代的路径大小不同。
例如,当前代码的输出:/somepath/example/hello-world.jspfirm.jspidgetsconfig.jsppjspdeals.jsp
我该如何解决这个问题,以便干净地打印和删除每个完整路径?
解决方案
推荐阅读
- flutter - Flutter:创建预定义的颜色抛出错误
- reactjs - React 受控组件不会像我预期的那样重新渲染/更新
- asp.net-core-mvc - Site.js 多久在 ASP.NET MVC 中读取一次?
- java - 如何使用替换方法将值存储在新数组中?
- javascript - 在 firestore (web) 中加入集合和子集合
- python - Tensorflow 错误:无法找到可以处理输入的数据适配器:
, 和内存错误 - docker - 如何在 Docker alpine 容器上安装 hwloc-dev 包
- java - LocalDate JPA 存储库奇怪的“功能”
- linux - 如何合并三个文件的内容(作为输入)并使用 shell 脚本显示
- mysql - mysql:连接两个表,然后拆分结果表的一列