**I am Proggraming in python however i have come to a slight glich which i cannot solve! The issue is that when it prints out in a text files it only prints one line of the whole ouput! Otherwise it works! Please i need help to make this work!

**

import sys, bz2, string, os
#get filename
filename = raw_input("Enter the path of bz2 document e.g. files/access_log-20130301.bz2: ")
#printfile which is going to be used
print "Using file : " + filename

source_file = bz2.BZ2File(filename, "r") 
for line in source_file:
    #Extract the date and put into a variable 
    logdate = string.split(line)[3][1:12]


# Extract movie name and put into variable movie #

    movie = string.split(line)[6]

# # extract who read the movie username = # #

    usernames = string.split(line)[2]

# #Only process the movie line if we have /media/movie in it.    #

    if movie.find('media/movies') > 0:

# #Prints all things prosscesed #

        print "User:" + usernames + " On:" +  logdate + " Was watching:"+ movie
        fp=open(filename+"record.txt", "w")
        fp.write("User: " + usernames + " On: " +  logdate + " Was watching: "+ movie+" File from:"+filename+"\n")

sys.exit()

Edited 3 Years Ago by deceptikon: Moved to the python forum

ive found the soloution

import sys, bz2, string, os


#instead of hardcoding filename, get it from arguments
#filename = os.getcwd()
filename = raw_input("Enter the path of bz2 document e.g. files/access_log-20130301.bz2: ")
print "Using file : " + filename
with open(filename+"record.txt", "wb+") as fp:
    source_file = bz2.BZ2File(filename, "r") 
    for line in source_file:
        #Extract the date and put into a variable 
        logdate = string.split(line)[3][1:12]
        #Extract movie name and put into variable movie
        movie = string.split(line)[6]
        #extract who read the movie username = 
        usernames = string.split(line)[2]
        #Only process the movie line if we have /media/movie in it. 
        if movie.find('media/movies') > 0:
            #Prints all things prosscesed
            print "User:" + usernames + " On:" +  logdate + " Was watching:"+ movie
            #p=open(filename+"record.txt", "w")
            fp.write("User: " + usernames + " On: " +  logdate + " Was watching: "+ movie+" File from:"+filename+"\n")
sys.exit()

Please mark the the thread "Solved" so as to not waste other's time looking at an already solved thread.

This article has been dead for over six months. Start a new discussion instead.