In Pythonwrite a function that opens the file weblogtxt and
In Python,write a function that opens the file \'weblog.txt\' and stores the entire contents of the file inside a string. (We can do this because the file is not tremendously large. If the file was huge, we should probably not store all of the text of the file inside a variable since that would eat up too much memory). Then ask the user to enter a string of text. Search for that text inside the file (i.e. the string you created above). Your function should output how many times the user\'s search string was present inside the file. Search for something like Status 401 which is an error that a web server log says to indicate that it couldn\'t find the resource the user was asking for. You should get back a value of 10.
NOTE: If, when you open the file, you get an error that says something about UnicodeDecodeError, modify your open() function so that it includes an additional \'encoding\' argument: open(the_file_name,\'r\', encoding=\'latin1\')
search Log Enter text to search for Status 401 10 search Log Enter text to search for Status 200 40 search Log Enter text to search for 64.242.88.10 47Solution
import os
import mmap #import memory mapped files
import re
\'\'\'
If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):
since file is considered to be large here
you can avoid the possible memory problems by using mmap.mmap() to create a \"string-like\" object that uses the underlying file (instead of reading the whole file in memory):
\'\'\'
def open_file():
filename = \'/yourfolderpath/weblog.txt\'
file_object = open(filename) #This inbuilt python function will open your file
s = mmap.mmap(file_object.fileno(),0,access = mmap.ACCESS_READ)
string_to_search = raw_input(\"Enter your input: \") # enter the string you want to search for
count=0 # number of times that string occurs
i=0
start=0
end = os.stat(filename).st_size #total size of file or last index of file
if(s.find(string_to_search)== -1) : # find function returns you the lowest index of the matched string otherwise -1
return 0 # return 0 when no match is present
while s.find(string_to_search,start,end) !=-1 :
count=count+1
start = s.find(string_to_search,start,end) #storing the last found index
start=start+1 # incrementing it by one as we have already searched till that index
s.close() # close mmap
file_object.close() #close file object
return count
\'\'\'
NOTE: in python 3, mmaps behave like bytearray objects rather than strings, so the subsequence you look for with find() has to be a bytes object rather than a string as well, eg. s.find(b\'string you searched for\'):
\'\'\'
if __name__ == \'__main__\':
count = open_file()
print count
