I am trying to figure out how to add a header to a .txt file which is a keystroke logger in python. Right now the problem with my code is that my header is displayed after every key is logged. But I need the header to be in the .txt file within its own line above the logged data. Additionally, for each .txt file created I need the header to be the same, but display I different time(I already have a time method for this ) For instance: this is how my .txt file currently looks with my code:
[START OF LOGS]
*~ Date/Time: Thu Aug 11 10:57:21 2022
t[START OF LOGS]
*~ Date/Time: Thu Aug 11 10:57:21 2022
e[START OF LOGS]
*~ Date/Time: Thu Aug 11 10:57:21 2022
s[START OF LOGS]
*~ Date/Time: Thu Aug 11 10:57:21 2022
t[START OF LOGS]
*~ Date/Time: Thu Aug 11 10:57:21 2022
This is how I need the .txt file to be shown. As you can see the header is above the logged data. I also need it to not interfere with the logged data
[START OF LOGS]
*~ Date/Time: Thu Aug 11 10:57:21 2022
test
Here are the variables used for the Header, it just collects the date and time from the time library
datetime = time.ctime(time.time())
Header = f'[START OF LOGS]\n *~ Date/Time: {datetime}\n'
this is the function that creates the .txt file. Currently the f.write(Header) is what's giving me the unwanted .txt file(first picture). Would I need to use a different method? I am calling the write_file functiion as a callback.
def write_file(keys):
with open(log.txt, "a") as f:
f.write(Header)
for key in keys:
k = str(key).replace("'", "")
# f.write(str(key))
if k.find("space") >= 0:
f.write('\n')
f.close()