Overwrite content of new generated file - python

The code generates a file every 30 seconds with the date/time and then types the 'key' in self.log into it. The problem is when the new file gets generated and 'key' gets typed into that it just appends itself to the bottom and doesn't overwrite the old content in the newly generated file.
Would appreciate your help :)
def report(self):
if self.log:
print(self.log)
starttime = time.time()
while True:
timestr = time.strftime("%Y%m%d-%H%M%S")
fileloc = f'C:/logging/pylogger/{timestr}.txt'
with open(fileloc, mode='w') as f:
f.seek(0)
f.truncate()
for key in self.log:
f.write(key)
time.sleep(30.0 - ((time.time() - starttime) % 30.0))

You question is not fully clear to me, but if I correctly understood you need to remove the elements from the list once written:
def report(self):
if self.log:
print(self.log)
starttime = time.time()
while True:
timestr = time.strftime("%Y%m%d-%H%M%S")
fileloc = f'C:/logging/pylogger/{timestr}.txt'
with open(fileloc, mode='w') as f:
f.seek(0)
f.truncate()
for key in self.log:
f.write(key)
self.log.remove(key)
time.sleep(30.0 - ((time.time() - starttime) % 30.0))
Just add self.log.remove(key) in your for loop, so the values are removed from the list once written and when you reach the next cycle after 30 seconds the new file will contains only the new values.

Related

Have each loop iteration create a new line of data in a file

I found and edited some code that records water flow data off of a flowmeter. I have managed to edit the script to run for only 5 mins and write to a file (I tried a .csv file but I am realizing this may not be entirely possible). However, when the script runs, the data file created just lists one "row" of recorded data. The while loop runs every 5 seconds for about 5 mins and provides a timestamp and a flow-rate reading, I'm trying to get the script to continuously record data. Here is the code:
import json
import time
from datetime import datetime, timedelta
import RPi.GPIO as GPIO
import csv
class FlowMeter():
''' Class representing the flow meter sensor which handles input pulses
and calculates current flow rate (L/min) measurement
'''
def __init__(self):
self.flow_rate = 0.0
self.last_time = datetime.now()
def pulseCallback(self, p):
''' Callback that is executed with each pulse
received from the sensor
'''
# Calculate the time difference since last pulse recieved
current_time = datetime.now()
diff = (current_time - self.last_time).total_seconds()
# Calculate current flow rate
hertz = 1. / diff
self.flow_rate = hertz / 7.5
# Reset time of last pulse
self.last_time = current_time
def getFlowRate(self):
''' Return the current flow rate measurement.
If a pulse has not been received in more than one second,
assume that flow has stopped and set flow rate to 0.0
'''
if (datetime.now() - self.last_time).total_seconds() > 1:
self.flow_rate = 0.0
return self.flow_rate
def main():
''' Main function for repeatedly collecting flow rate measurements
and sending them to the SORACOM API
'''
# Configure GPIO pins
INPUT_PIN = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(INPUT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Init FlowMeter instance and pulse callback
flow_meter = FlowMeter()
GPIO.add_event_detect(INPUT_PIN,
GPIO.RISING,
callback=flow_meter.pulseCallback,
bouncetime=20)
end_time = datetime.now() + timedelta(minutes=5)
while True:
timestamp = str(datetime.now())
flow_rate = flow_meter.getFlowRate()
time.sleep(5)
print('Timestamp: %s' % timestamp)
print('Flow rate: %f' % flow_rate)
header = ['Time','Flow Rate']
data = [timestamp, flow_rate]
with open('flowrate.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerow(data)
if datetime.now() > end_time:
break
if __name__ == '__main__':
main()
I've included my "data writing" strings near the bottom in the while loop.
Opening the file with mode 'w' overwrites the whole file every time. So, just write the header at the beginning, and "append" later on:
def main()
with open('flowrate.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
... later, in the loop ...
with open('flowrate.csv', 'a', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(data)
This is only practical if you're really only writing a row every now and then. If you're writing more often, then you should just keep the file open and reuse the writer instance.
Your problem is you're opening the file and effectively truncating (rewriting) it each time. Instead, open the file, then run the loop.
with open('flowrate.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
while True:
timestamp = str(datetime.now())
flow_rate = flow_meter.getFlowRate()
time.sleep(5)
print('Timestamp: %s' % timestamp)
print('Flow rate: %f' % flow_rate)
header = ['Time','Flow Rate']
data = [timestamp, flow_rate]
writer.writerow(data)
if datetime.now() > end_time:
break

Is there a method to print the duration of the script and save it inside the csv?

I would like to print the duration of the script and save it inside the csv. What would be the best method?
import requests
with open('infile.txt', 'r') as f:
urls = f.readlines()
datalist=[]
for url in urls:
data = requests.get(url)
datalist.append(data.text)
with open('outfile.txt', 'w') as f:
for item in datalist:
f.write("%s\n" % item)
You can use datetime module.
import requests
from datetime import datetime
def run():
with open('infile.txt', 'r') as f:
urls = f.readlines()
datalist=[]
for url in urls:
data = requests.get(url)
datalist.append(data.text)
with open('outfile.txt', 'w') as f:
for item in datalist:
f.write("%s\n" % item)
startTime = datetime.now()
run()
print(datetime.now() - startTime)
One simple way you could do this is by using the built-in time module. Get the time before the execution, get the time after the execution, subtract them and you get the time taken for your script to run. You could then just export it to your csv just like every other text.
import time
# STEP 1: Get start time
start_time = time.time()
# Step 2: Run your code you want to time
result = func_one(some_param)
# Step 3: Calculate total time elapsed
end_time = time.time() - start_time

Python script need to save output to text file

I have peiced together some code from the internet to capture pressed keys and the current active window title and am trying to write the output of the python script to a text file.
The script works fine in the IDLE console and prints pressed keys and logs any change in the current active window.
from pynput.keyboard import Key, Listener
import time
from win32gui import GetWindowText, GetForegroundWindow
import datetime
from threading import Thread
def on_press(key):
print ('{0} pressed'.format(key))
def on_release(key):
('{0} release'.format(key))
if key == Key.esc:
return False
def get_titles():
current_title = None
while True:
moment2 = datetime.datetime.now().strftime("%d-%b-%Y [ %H:%M:%S ]")
new_title = GetWindowText(GetForegroundWindow())
if new_title != current_title:
if len(new_title) > 0:
#logging.info(" Moved to : " + new_title)
current_title = new_title
time.sleep(0.1)
#print(new_title)
ff= (moment2 + " : " + "Moved T0 : "+ new_title)
print (ff)
I am looking for a simple way to write the outputs i can see in the console to a text file. It is probably very simple but i am very much a beginner. Thanks
Python has a native open() function, no import needed, which allows you to handle files. This function "loads" the file into memory, and can be set into various modes:
open("filename.txt", "a"), appending the content to a new line;
open("filename.txt", "w"), overwriting the content; and
open("filename.txt", "r"), setting it to read-only.
open("filename.txt", "x"), to create a file.
You can add a "+" to each of this modes ("a+", "w+"), if you want the file to be created if it doesn't already exist.
You define the file in memory to a variable as such: a = open("filename.txt", "w"), and can then text = a.read() to load the content of the file to a string, or a.readlines() to load the strings into an array, split per \n.
Use a.write("Your desired output") to save the content to the file, if the file is in write or append modus.
Edit:
Try to only open files for as long as they are actually needed.
with open("filename.txt", "r") as f:
file_contents = f.read()
# file_contents = "This file contains\nvery important information"
file_lines = f.readlines()
# file_lines = ["This file contains", "very important information"]
# Similar to file_lines = file_contents.split("\n")
in order to avoid blocking other parts of your program, and avoid corrupting your files if Python crashes unexpectedly.
Just add
with open('textfile.txt', 'a+') as f:
f.write(ff)
a option is for appending to a file and + means that if the file is not there just create one with the specified name.
EDIT:
def on_press(key):
print ('{0} pressed'.format(key))
with open('textfile.txt', 'a+') as f:
f.write(ff)
EDIT 2:
def on_press(key):
print ('{0} pressed'.format(key))
k = key + '\n'
with open('textfile.txt', 'a+') as f:
f.write(key)
# and in get_titles()
ff= (moment2 + " : " + "Moved T0 : "+ new_title + '\n')
with open('textfile.txt', 'a+') as f:
f.write(ff)
try this when run program in console
python your_script.py > path_to_output_file/outpot.txt
in case '>' not work then try '>>'

How to keep writing value into spreadsheet in a user controlled loop in Python?

I'm trying to write values into a .csv file every second until interrupted by the user by pressing a key. I'm able to achieve it in a finite loop.
I tried using raw_input but the program would only write the last value before interruption. What should I change in my code?
Here's the code that works for a finite loop:
import time
import csv
class Timestamp:
def __init__(self):
my_file = open('test_csv.csv','w+')
with my_file:
new_file = csv.writer(my_file)
for val in range(0,20):
with open('test_csv.csv','a') as f:
date_now = time.strftime('%d/%m/%y')
time_now = time.strftime('%H:%M:%S')
to_write = [date_now, time_now]
csv_file =csv.writer(f)
csv_file.writerow(to_write)
time.sleep(1)
Timestamp()
You can use threads.
import time
import csv
from threading import Thread
def write_loop(self):
my_file = open('test_csv.csv', 'w+')
with my_file:
new_file = csv.writer(my_file)
for val in range(0, 20):
with open('test_csv.csv', 'a') as f:
date_now = time.strftime('%d/%m/%y')
time_now = time.strftime('%H:%M:%S')
to_write = [date_now, time_now]
csv_file = csv.writer(f)
csv_file.writerow(to_write)
time.sleep(1)
t = Thread(target=write_loop)
t.daemon = True
t.start()
input("Press any key to stop")

Python save output line into new file

I get some lines (a record) from log file, but I didn't know how to write function inside it to create a new log file whose name contains the current date in which to store those lines. I'm new python, so I hope you can give me solution. Thanks
def OnlyRecent(line):
if time.strptime(line.split("[")[0].strip(),"%a %b %d %H:%M:%S %Y")> time.gmtime(time.time()-(60*60*24*7)):
return True
return False
for line in f:
if OnlyRecent(line):
print line //store print lines into new log file. 20120911.log
You can redirect print output to a file:
log_file = open('mylog.log', 'w')
print>>log_file, 'hello'
log_file.close()
outside your loop
filename = time.strftime('%Y%m%d') + '.log'
f = open(filename, 'w')
do your loop here, and write each line with
f.write(line)
if you don't have a new line character in your line variable, do
f.write(line +'\n')
after exiting your loop
f.close()

Categories