File I/O python

-5

I am trying to find few strings in a file but the line.find() doesn't return true for any string in the file.Please have a look an suggest something.The search has to be sequential and I need to hold the offset value for every string which is found. and the search for next string should start from that offset.

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

The Ouput :

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

Here is the sample File:

>    [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
python
python-2.7
file
file-io
asked on Stack Overflow Sep 15, 2013 by user2781569 • edited Jul 16, 2019 by Martijn Pieters

1 Answer

1

You are using the wrong test; use in to test for values in a line:

if Search in line:

line.find(Search) will only be true if either the value of Search is not in the line or if it is in the line at a position other than the start.

str.find() returns -1 if the value is not found, or the integer position otherwise. That means that if line starts with the value of Search, 0 is returned, and 0 tests as false in a boolean context such as 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

Next, for any line in the file your test returns False, you then close the file:

else:
   fh.close()

which will terminate the loop early; most lines will not match your test, you really don't want to close the file that quickly.

You also always execute the line return "Could not find String %s"%(Search), 1; you want to test if Flag is False:

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

You can restructure the search to use the else branch of a for loop:

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

The break prevents the else block from running. The with block takes care of closing the file for you, using the file object as a context manager.

answered on Stack Overflow Sep 15, 2013 by Martijn Pieters • edited Sep 15, 2013 by Martijn Pieters

User contributions licensed under CC BY-SA 3.0