Windows 严格搜索两个不同的文件

Windows 严格搜索两个不同的文件,windows,python-2.7,Windows,Python 2.7,关于以下代码,我有两个问题: import subprocess macSource1 = (r"\\Server\path\name\here\dhcp-dump.txt") macSource2 = (r"\\Server\path\name\here\dhcp-dump-ops.txt") with open (r"specific-pcs.txt") as file: line = [] for line in file: pcName = line.

关于以下代码,我有两个问题:

import subprocess

macSource1 = (r"\\Server\path\name\here\dhcp-dump.txt")
macSource2 = (r"\\Server\path\name\here\dhcp-dump-ops.txt")

with open (r"specific-pcs.txt") as file:
    line = []
    for line in file:
        pcName = line.strip().upper()
        with open (macSource1) as source1, open (macSource2) as source2:
            items = []
            for items in source1:
                if pcName in items:
                    items_split = items.rstrip("\n").split('\t')
                    ip = items_split[0]
                    mac = items_split[4]
                    mac2 = ':'.join(s.encode('hex') for s in mac.decode('hex')).lower()  # Puts the :'s between the pairs.
                    print mac2
                    print pcName
                    print ip
首先,如您所见,脚本正在根据macSource1的内容搜索“specific-pcs.txt”的内容,以获得各种详细信息。如何让它同时搜索macSource1和MacSource2(因为详细信息可能在任何一个文件中)

其次,我需要有一个更严格的匹配过程,因为目前一台名为“itroom02”的机器不仅会找到它自己的详细信息,而且还会提供另一台名为“2nd-itroom02”的机器的详细信息。我怎么才能得到呢

非常感谢您的帮助!
克里斯。

也许你应该把它重组得更像这样:

macSources = [ r"\\Server\path\name\here\dhcp-dump.txt",
               r"\\Server\path\name\here\dhcp-dump-ops.txt" ]

with open (r"specific-pcs.txt") as file:
    for line in file:
        # ....
        for target in macSources:
            with open (target) as source:
                for items in source:
                   # ....
无需在您对“行输入…”执行
之前立即执行例如
line=[]


至于“更严格的匹配”,由于您没有给出文件格式的示例,我只能猜测-但您可能希望在完成拆分后尝试类似于
if items\u split[1]==pcName:
的操作,而不是在拆分前尝试
if pcName in items:
(假设名称在第二列-如果不在,则相应调整).

谢谢@twalberg,我喜欢这个简单的修复程序,就像你的例子一样——效果非常好。对不起,是的,txt文件的格式基本上只是从dhcp服务器导出的一个标签删除列表,所以是一列ip地址,然后是pc名称、mac地址和一些其他垃圾。实际上,想想看,txt文件上的pc名称是FQDN,所以pcName.domain,所以一个简单的item=pcName可能不起作用…在这种情况下,也许
items\u split[1].startswith(pcName+')。
可能是一个合适的选择…谢谢@twalberg,你出色地回答了我的两个问题。