0

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.

4
  • What you're doing with fileline = foo, bar, baz is setting fileline to a tuple of three values. You can't write a tuple, only a string. (The fact that print can take a bunch of comma-separated arguments is because print is a special statement.) Commented Jul 28, 2014 at 19:08
  • why are you opening and then using seek? Commented Jul 28, 2014 at 19:09
  • Is there a reason you're trying to write your own logger instead of using the logging module? 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). Commented Jul 28, 2014 at 19:12
  • @abarnet just because you see only 6 lines of code doesnt mean thats all thats there. I have it in a function, and a bunch of other code. I just took the snippet that wasn't working. Python isnt the first programming language I know. Dont judge a book by its cover yo but thanks for the tip Commented Jul 28, 2014 at 19:17

1 Answer 1

1

You can format your data into a string; with string formatting, for example:

firstline = "Log Created: {}/nLog deleted and recreated.".format(grab_date)

You can also make use of print()'s ability to convert all arguments to a string and automatic newlines, and have it write to the file:

print("Log Created:", grab_date, file=f)
print("Log deleted and recreated.", file=f)

If you can avoid it, don't reinvent the logging wheel and use the logging module. It can be configured to take a different date format:

>>> import logging
>>> logging.basicConfig(datefmt="%A %d, %B %Y %I:%M:%S %p %Z", format='Log Created: %(asctime)-15s %(message)s')
>>> logging.warn('Foo bar baz!')
Log Created: Monday 28, July 2014 08:13:44 PM BST Foo bar baz!
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help! Would it be worth using logging when all im really adding to the text file is just a few errors?
@catalyst: sure, logging is easy to use and gives you a lot of flexibility later when you may need more control. The module gives you that for free.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.