文件I/O python

文件I/O python,python,python-2.7,file,file-io,Python,Python 2.7,File,File Io,我试图在一个文件中找到几个字符串,但是对于文件中的任何字符串,line.find()都不会返回true。请看一看并提出建议。搜索必须是连续的,我需要保留找到的每个字符串的偏移量值。搜索下一个字符串应该从该偏移量开始 def CheckFile(*argv): import os Filename = argv[0] Search = argv[1] Flag = False FileFlag = False offset1 = 0 offset2 = 0 if o

我试图在一个文件中找到几个字符串,但是对于文件中的任何字符串,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

你使用了错误的测试;使用中的
测试行中的值:

if Search in line:
line.find(Search)
仅当
Search
的值在该行中为或位于该行的起始位置以外时才为真

str.find()
如果找不到值,则返回
-1
,否则返回整数位置。这意味着如果
搜索
的值开始,则返回
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()
这将提前终止循环;大多数行与您的测试不匹配,您确实不想那么快地关闭文件

您还始终执行行
返回“找不到字符串%s%”(搜索),1
;您想测试
标志
是否为

if not Flag:
    return "Could not find String %s"%(Search), 1
您可以重新构造搜索以使用
for
循环的
else
分支:

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
块运行。<代码>使用Bug,将文件关闭为文件管理器。使用/file >您在哪个输入中寻找哪些字符串?也可以考虑让代码少一些意大利面。如果您将代码拆分为文件I/O(打开文件,检查它是否存在,yadda等等)和搜索算法(只在行上或行/偏移元组上使用迭代器),那么调试会更容易。这样,您至少可以定位错误,而不是在上面堆一堆代码。我正在寻找一个名为“NETBIOS”的字符串这已经存在于文件中了..我是否也应该附上文件..我是新来的,还在学习中!你的凹痕好像脱落了;尤其是在你的回路周围!谢谢你指出这一点…但这只是一个复制粘贴错误…我也尝试过这个…与上面提到的不起作用code@user2781569:您确定在所有位置都已更正吗?现在看来,
line.find()
肯定是错误的。@user2781569:请注意,str
中的
str.find()
都不会不敏感地搜索大小写;如果您正在查找
hello
,但字符串包含
hello
,则也不会进行匹配。进行了建议的更改…以下是我得到的>>检查文件(“D:\wireshark.txt”,“NetBIOS”)(“找不到字符串NetBIOS”,1)@user2781569:您可以编辑您的问题。添加该信息,以及显示存在
NetBIOS
的文件中的示例。