-5

我正在尝试在文件中查找几个字符串,但 line.find() 不会为文件中的任何字符串返回 true。请看一下建议的内容。搜索必须是连续的,我需要保留偏移量找到的每个字符串的值。并且搜索下一个字符串应该从该偏移量开始。

def CheckFile(*argv):
  import os
  Filename = argv[0]
  Search = argv[1]
  Flag = False
  FileFlag = False
  offset1 = 0
  offset2 = 0
  if os.path.exists(Filename) == 0:
    return "File Doesn't exist", 1
  else:
    fh = open(Filename,"r")
    for line in fh:
      if Search in line:
        print "Success"
        print (line)
        Flag = True
        offset1 = fh.tell()
        #offset1 = int(offset1)
        break
      else:
        fh.close()
        return "Could not find String %s"%(Search), 1
        #fh.close()
    if Flag:
      fh = open(Filename,"r")
      print(offset1)
      fh.seek(offset1)
      for line in fh:
        if "TestDir1\TestFile1.txt" in line:
          print "Success"
          print (line)   
          FileFlag = True
          offset2 = fh.tell()
          #offset2 = int(offset2)
          break
        else:
          fh.close()
          return "Couldn't Find File TestDir1\TestFile1.txt", 1
          #fh.close()
    if Flag and FileFlag:
      fh = open(Filename,"r")
      print(offset2)
      fh.seek(offset2)
      for line in fh:
        if "Persistent Handle: True" in line:
          print "Success"
          return "Success -- Found the strings", 0
        else:
          fh.close()
          return "Failur -- Failed to find 'Persistent Handle: True'", 1

输出:

>>> CheckFile("D:\wireshark.txt","NetBIOS")
('Could not find String NetBIOS', 1)

这是示例文件:

>    [SEQ/ACK analysis]
>        [This is an ACK to the segment in frame: 104]
>        [The RTT to ACK the segment was: 0.043579000 seconds]
>        [Bytes in flight: 252]
>NetBIOS Session Service
>    Message Type: Session message (0x00)
>    Length: 248
>SMB2 (Server Message Block Protocol version 2)
>    SMB2 Header
>        Server Component: SMB2
>        Header Length: 64
>        Credit Charge: 1
>        Channel Sequence: 0
        Reserved: 0000
        Command: Create (5)
        Credits requested: 1
        Flags: 0x00000000
4

1 回答 1

1

您使用了错误的测试;用于in测试一行中的值:

if Search in line:

line.find(Search)Search仅当 的值不在行中或在行中的起始位置以外的位置时才会为真。

str.find()如果未找到该值,则返回-1,否则返回整数位置。这意味着如果line , 的值开头Search0则返回,并0在布尔上下文中测试为 false,例如if

>>> 'hello'.find('hello')
0
>>> if 'hello world'.find('hello'):
...     print 'Found but not found?'
... else:
...     print 'That did not come out the way you thought it would'
... 
That did not come out the way you thought it would
>>> 'hello' in 'hello world'
True

接下来,对于测试返回的文件中的任何行False,然后关闭文件:

else:
   fh.close()

这将提前终止循环;大多数行与您的测试不匹配,您真的不想那么快关闭文件。

你也总是执行这条线return "Could not find String %s"%(Search), 1;你想测试是否FlagFalse

if not Flag:
    return "Could not find String %s"%(Search), 1

您可以重组搜索以使用循环的else分支:for

with open(Filename,"r") as fh:
    for line in fh:
        if Search in line:
            print "Success"
            print (line)
            offset1 = fh.tell()
            break
    else:
        return "Could not find String %s"%(Search), 1

break阻止else块运行。该with块负责为您关闭文件,使用文件对象作为上下文管理器。

于 2013-09-15T19:16:43.980 回答