I am trying to write a string + datetime to a text file with Python. I'm trying to make a basic logger, that will log the datetime in a format of; "Full weekday, day number, Full month name, full year, 12 hour format, minute, second, AM/PM, UTC". I read that file.write() can only take strings and not integers or dates etc. So how would I would I make this work to be written to the text file?
grab_date = datetime.datetime.now().strftime("%A %d, %B %Y %I:%M:%S %p %Z")
firstline = "Log Created: ", grab_date, "/nLog deleted and recreated."
f = open("gen-log.txt", "w")
f.seek(0)
f.write(firstline)
f.close()
EDIT: not quite sure why but I read on one post that f.seek(0) did something and it worked for them. lol. I just left it in there. Sorry.
fileline = foo, bar, bazis settingfilelineto a tuple of three values. You can'twritea tuple, only a string. (The fact thatprintcan take a bunch of comma-separated arguments is becauseprintis a special statement.)loggingmodule? There are a whole lot of other things to deal with that you haven't even begun to approach (like appending instead of overwriting and/or moving the old file out of the way, and wrapping things up in a single function call so you don't have to write 6 lines of code for each log message, and deciding whether to keep the file open or reopen/close with each message or batch them up, and so on).