Python Forum
What does .flush do? How can I change this to write to the file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does .flush do? How can I change this to write to the file?
#1
Looking at using yield, I have this simulated log file: logsim.py

logsim.py has 2 very long lists, ips and docs

ips like this: ips = ['129.194.8.73', '198.37.27.153', '217.127.12.71', ...
docs like this: docs = ['"GET /ply/ply-2.0.tar.gz HTTP/1.0" 304 -',
'"GET /python/tutorial/beazley_advanced_python/Slides/SLIDE066.HTM HTTP/1.0" 200 1538', ....

The tutorial is here.

logsim.py code, apart from the lists ips and docs:

import time, random
# a fake log programme

f = open("/home/pedro/temp/access-log.log","w")

while True:
    time.sleep(random.random())
    n = random.randint(0,len(ips)-1)
    m = random.randint(0,len(docs)-1)
    t = time.time()
    date = time.strftime("[%d/%b/%Y:%H:%M:%S -0600]",time.localtime(t))
    #print(f,"%s - - %s %s" % (ips[n],date,docs[m]))
    print(f'{f} {ips[n]},{date},{docs[m]}')      
    f.flush()
For the tutorial it says, leave this running in the background, so I run it in bash.
But nothing is written to /home/pedro/temp/access-log.log when I run this in bash

Should I change f.flush() for f.write(ips[n] + ',' + date + ',' + docs[m])

Often, the file does not change until f.close() is called, but I can't do that, because logsim.py is still writing to f!

How to get the data actually written to /home/pedro/temp/access-log.log without closing the file?
How to dynamically update the file, without closing it?
Reply


Messages In This Thread
What does .flush do? How can I change this to write to the file? - by Pedroski55 - Apr-22-2024, 09:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 506 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,669 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,627 Nov-09-2023, 10:56 AM
Last Post: mg24
  logging: change log file permission with RotatingFileHandler erg 0 1,125 Aug-09-2023, 01:24 PM
Last Post: erg
  How can I change the uuid name of a file to his original file? MaddoxMB 2 1,004 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  How do I read and write a binary file in Python? blackears 6 7,342 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,160 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,720 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 999 Feb-15-2023, 05:34 PM
Last Post: zsousa
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,488 Jan-21-2023, 06:57 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020