Python Forum
save data in .txt after certain interval
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
save data in .txt after certain interval
#1
Hi Everyone,

Here is just snippet of my code to tell exact my requirement and not my actual code.

import time, datetime

ctr=100

opn = open("log.txt","a+")

while(ctr):
    opn.writelines(str(ctr))
    ctr=ctr-1
    time.sleep(1)
    print(ctr)

opn.close() #close file
I am working on automating a process in which I need to save the data after certain interval of time.
Above method works but data will store only after the file closes. Also, I can not get the data in file if user interrupt the process.

Is there any way I can save the log after every interval before "opn.close" instruction? Guide me if I am wrong as I already read the details of "OPEN" in Python manual

Thanks
Reply
#2
Use the flush() method. Also your use of writelines is not correct
import time, datetime
 
ctr=100
 
opn = open("paillasse/ecrlog.txt","a+")
 
while(ctr):
    opn.write(str(ctr) + '\n')
    opn.flush()
    ctr=ctr-1
    time.sleep(1)
    print(ctr)
 
opn.close() #close file
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Mapping a value to an interval JazonKuer 12 1,841 Mar-17-2023, 07:59 PM
Last Post: Gribouillis
  Confidence interval after doing penalised cox regression HatemAli 0 1,110 Feb-23-2022, 11:02 PM
Last Post: HatemAli
  Save data frame to .csv df.to.csv() mcva 1 1,496 Feb-03-2022, 07:05 PM
Last Post: mcva
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,115 Jul-02-2021, 02:19 PM
Last Post: xtc14
  How to save json data in a dataframe shantanu97 1 2,121 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Complex X Tick Interval JoeDainton123 0 1,443 Oct-05-2020, 07:27 PM
Last Post: JoeDainton123
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,405 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  How to save Python Requests data sent to server? RedLeonard 5 4,796 Jul-05-2020, 10:33 AM
Last Post: RedLeonard
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,079 Jun-26-2020, 11:59 AM
Last Post: Mangesh121

Forum Jump:

User Panel Messages

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