I have this example code of a program I am writing at the place I work as a 'time clock' of sorts. I want to come in, run the program and clock in. When I clock in, I want to write this to a csv file so I can send it to payroll and get paid. The thing is I am unsure as to how to write this code to update to the next line. Every time I write to the csv, it overwrites the current line in the file. Any ideas?

writer = csv.writer(open('test.csv', 'w'), delimiter=' ')
        date = str(today.month) + "/" + str(today.day) + "/"  + str(today.year)
        writer.writerow(date)

I understand that it's not the most efficient code, especially the 'date' line, but I just want it to work, I'll make it look nice later.

Also, I don't want to read the file, then rewrite it (as was suggested to me in another forum) because I will be keeping a record of all of my dates while working here, and that just doesn't seem efficient to me. Thanks!

Dani AI

Generated

As pointed out, the overwrite is because the file was opened for writing instead of appending. Open the CSV in append mode and use a context manager so the file is closed reliably. Also be aware of CSV-related platform quirks: on Python 3 open with newline='' to avoid extra blank rows on Windows; on Python 2 use binary append mode.

A minimal, practical example (Python 3) that writes one timestamp per row and treats the whole timestamp as a single field:

import csv
from datetime import datetime

ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("test.csv", "a", newline="") as fh:
    csv.writer(fh).writerow([ts])

If you still run Python 2, open in binary append ("ab") and encode text if needed.

A few gotchas and extras:

  • Pass a sequence (e.g., a one-item list) to writerow; if you pass a plain string it will be split into characters and produce multiple fields.
  • Prefer standard CSV conventions (comma delimiter) unless your payroll system expects space-separated values.
  • If you want a header, check for file existence and write the header only when creating the file.
  • For concurrent writes (multiple processes or network shares) add file locking or centralize collection to avoid race conditions.
  • Use datetime.strftime (as suggested via strftime) rather than manual concatenation for robust, locale-stable output.

This resolves the overwriting and gives you clean, one-line-per-clock-in CSV rows suitable for payroll.

Recommended Answers

All 4 Replies

When you open your file you should be opening it in append mode using the 'a' flag (right now you're opening it in write mode, 'w').

Wow, I feel dumb :sweat: Thanks though!

just a point of interest have a look at the time() module it has very nifty function called strftime, then you could have something like this:

date = time.strftime('%m/%d/%Y',time.localtime())

Ahh thanks! I never noticed that one. I've just been using:

today = datetime.datetime.now()

And then breaking it down as I did above. This makes things a little easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.