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!

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.