如果一个fi的行中包含两个字符串

2024-10-03 11:14:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,所以请容忍我,如果已经讨论过这个主题,请提前道歉:)

我正在编写一个脚本来检查来自某个域的SMTP日志列表。我想忽略任何包含SMTP会话其他部分(如数据或RCPT等)的行

以下是脚本:

import os
files = os.listdir('t:\smtpsvc1')
path = "t:\smtpsvc1" # I have used this variable as os.path.join wouldn't accept the variable 'files'
for f in files:
    fullpath = os.path.join(path,f)
    print(fullpath)
    searchfile = open(fullpath, "r")
    for line in searchfile:
            if ('FROM' and '@somedomain.local' in line):
                print (line)
searchfile.close()

脚本运行,打印第一个文件的名称,然后检查行并输出如下数据:

2016-09-28 09:31:50 192.168.106.28应用服务器SMTPSVC1 SMTPSERVER 192.168.106.124 0数据-+250 0 139 612 47 SMTP--

这似乎触发了查找@somedomain.local的条件,但似乎忽略了if语句的FROM部分。 我做错了什么? 提前欢呼


Tags: 数据pathin脚本foroslinefiles
1条回答
网友
1楼 · 发布于 2024-10-03 11:14:36

更改此项:

if 'FROM' and '@somedomain.local' in line:

为此:

if 'FROM' in line or '@somedomain.local' in line:

我假设您要查找包含其中一个的行

如果您实际上正在查找同时包含两个的行,则将or更改为and

相关问题 更多 >