File I/O python
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 line.find(Search):
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 line.find("TestDir1\TestFile1.txt"):
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 line.find("Persistent Handle: True"):
print "Success"
return "Success -- Found the strings", 0
else:
fh.close()
return "Failur -- Failed to find 'Persistent Handle: True'", 1
No comments:
Post a Comment