| | |
File I/O Problem: Incomplete or Missing Content During Writeline
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
I am new to python and posting to this forum and my searches turned up nothing. I am writing a component for a log parser to combine events from matching dates into one file. For some reason the log files split between 19:59:00 and 20:00:00 hours daily. To resolve this the code sample below reads the previous event count from a file based on the date being processed. Next the event number is incremented and added to each of the events and the merged lines are written to a file.
The problem I am having is some of the last events between 10 and 50 are either not written to the file or partially written. When using the interactive mode, while printing each line they are displayed correctly. I have tried using fileinput and read both produce the same results for each date. Using write instead of writelines also did not change the results.
Any suggestions are greatly appreciated.
Thanks in advance
Mece
Event Sample
FIREWALL-1 2008-10-02 20:00:04 Deny proto=17 192.168.10.90 123 172.15.6.5 123
last_count sample
2008-10-01 628756
The problem I am having is some of the last events between 10 and 50 are either not written to the file or partially written. When using the interactive mode, while printing each line they are displayed correctly. I have tried using fileinput and read both produce the same results for each date. Using write instead of writelines also did not change the results.
Any suggestions are greatly appreciated.
Thanks in advance
Mece
Event Sample
FIREWALL-1 2008-10-02 20:00:04 Deny proto=17 192.168.10.90 123 172.15.6.5 123
last_count sample
2008-10-01 628756
Python Syntax (Toggle Plain Text)
count_file = open("/home/user/logs/last_count","r") other_date = "2008-10-01" for line in count_file.readlines(): if other_date in line: line = line.split() prev_event_count = line[1] more_logs = file("/home/user/logs"+other_date+"-DENY.log-cont","w") new_log_data = "/home/user/logs"+other_date+"-DENY.log-cont" for line_data in open(data_file,"r"): prev_event_count = int(prev_event_count) + 1 nudata = str(prev_event_count),str(line_data) new_data = ' '.join(nudata) more_logs.write(new_data)
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
Sorry about that, I updated the code
Python Syntax (Toggle Plain Text)
count_file = open("/home/user/logs/last_count","r") data_file = "/home/user/home/prev-date-logs" other_date = "2008-10-01" for line in count_file.readlines(): if other_date in line: line = line.split() prev_event_count = line[1] more_logs = file("/home/user/logs"+other_date+"-DENY.log-cont","w") new_log_data = "/home/user/logs"+other_date+"-DENY.log-cont" for line_data in open(data_file,"r"): prev_event_count = int(prev_event_count) + 1 nudata = str(prev_event_count),str(line_data) new_data = ' '.join(nudata) more_logs.write(new_data) more_logs.write('\n')
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
Thanks for the suggestion, but flush did not work. Here is the updated code, is my syntax correct?
I also tried moving the flush statement to the beginning and end of the loop, in addition to trying when the loop is completed. I tried this both in read and fileinput.
Python Syntax (Toggle Plain Text)
count_file = open("/home/user/logs/last_count","r") data_file = "/home/user/home/prev-date-logs" other_date = "2008-10-01" for line in count_file.readlines(): if other_date in line: line = line.split() prev_event_count = line[1] more_logs = file("/home/user/logs"+other_date+"-DENY.log-cont","w") new_log_data = "/home/user/logs"+other_date+"-DENY.log-cont" for line_data in open(data_file,"r"): prev_event_count = int(prev_event_count) + 1 nudata = str(prev_event_count),str(line_data) new_data = ' '.join(nudata) more_logs.write(new_data) more_logs.flush()
I also tried moving the flush statement to the beginning and end of the loop, in addition to trying when the loop is completed. I tried this both in read and fileinput.
Try this
python Syntax (Toggle Plain Text)
for line ... if other_date ... ... more_logs = open(..., "w") try: new_log_data ... for line_data ... ... more_logs.write(new_data) finally: more_log.close()
Last edited by Gribouillis; Oct 31st, 2008 at 1:44 pm.
![]() |
Other Threads in the Python Forum
- Previous Thread: Beginner Code
- Next Thread: Python (Django) vs PHP
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib





