文件I / O python

时间:2013-09-15 19:14:06

标签: python python-2.7

我试图在文件中找到几个字符串但是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

1 个答案:

答案 0 :(得分:1)

您使用的是错误的测试;使用in来测试一行中的值:

if Search in line:

line.find(Search)只有在行中Search的值或者位于行开头以外的行中时才会为真。 / p> 如果找不到值,则

str.find()返回-1,否则返回整数位置。这意味着,如果line 启动且值为Search,则返回0,并且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块负责为您关闭文件。

相关问题