python-3.x - 在循环内仅打印一次输出
问题描述
for i in os.listdir(path_1):
for j in os.listdir(path_2):
file_name = (j.split('.')[0])
if i.__contains__(file_name) and i.endswith('txt'):
txt_tym = os.path.getctime(path_1 + '/' + i)
log_tym = os.path.getctime(path_2 + '/' + j)
if txt_tym >= log_tym:
print('Issues found in: '+i)
else:
print('No issues found')
我正在使用这个程序来比较两个不同目录中两个文件之间的时间戳,它具有相同的名称但不同的扩展名,
我需要在文本文档中显示结果。如果有问题,它将打印Isues found in: filename
。仅当没有单个文件存在问题时,我才
需要打印,我在循环内使用 else 并且它会打印多次。请对此提出一些建议 No issues found
解决方案
像这样的东西应该工作:
issues_found = false
for i in os.listdir(path_1):
for j in os.listdir(path_2):
file_name = (j.split('.')[0])
if i.__contains__(file_name) and i.endswith('txt'):
txt_tym = os.path.getctime(path_1 + '/' + i)
log_tym = os.path.getctime(path_2 + '/' + j)
if txt_tym >= log_tym:
print('Issues found in: '+i)
issues_found = true
if not issues_found:
print('No issues found')
推荐阅读
- python - ValueError:找不到资源“C:\Users\loginname\Desktop\2020\chatbot\.data\data.json”
- javascript - 使用 $.append 时的 jQuery 脚本标签执行顺序
- flutter - 在 Flutter 中如何创建数据库和搜索功能?
- hibernate - Hibernate 5,具有 2 个限制的 where 子句的条件查询
- linux - 在linux中使用berkley数据包过滤器获取接口统计信息?
- javascript - 如何在 tabulator.js 中向嵌套表中添加新行
- python - 有没有办法在后台 Windows 终端中运行一个 jumpmotion python 脚本?
- uwp - 运行批处理脚本时未创建快捷方式
- scala - 类型系统示例的 Scala 语法/语法?
- lua - 如何在 timer.performWithDelay 之后运行代码?- 电晕SDK