0

I have the following:

with open(file, 'a') as log:

   #A bunch of code, some of it writes to log.

   log.seek(0)
   log.write(time.strftime(t_format))

seek() doesn't work with append, and if I use 'w', then the beginning of the file gets overwritten. In the docs it says, "...'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position)"

Is there any way to override this?

bmikolaj
  • 485
  • 1
  • 5
  • 16
  • 4
    It's impossible to append to the beginning of a file on any filesystem I've heard of. You'll need to write a new file with the stuff you wanted to "append" and then add on what was in the file to begin with. – Wooble Oct 17 '14 at 13:24

1 Answers1

0

The second argument to seek allows seeking relative to the end of the file:

with open(filename, 'w') as log:
    log.seek(0, 0)
    log.write(time.strftime(t_format))

You should also consider using Python's logging module to write logs.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48