0

I am using Python (Raspberry Pi) to edit a CSV file. I open the file with the "append" tag and add a line. After that I check the size of the fil. If the file size is too big, I want to delete the first row of data (there is a headers row). Every example I see just skips the row and the writes all the other rows to a different CSV file. I do not want to have to create a new CSV file...I just want to delete the first row in the current file and save it.

import csv

def write_csv(datalist):   

    with open("CSVfile",'a') as f:
        writer = csv.writer(f, delimiter=',' ,quoting=csv.QUOTE_NONE)
        writer.writerow(datalist)

    while os.path.getsize("CSVfile") > 100000:
        ****DELETE FIRST ROW OF DATA FROM CSV FILE****
9
  • You will have to repeatedly save it again. Each time with the first row of datalist removed, until the size is below 100000. Commented Apr 15, 2016 at 18:57
  • This code has an error, there is no writeline for the csv.writer object. Commented Apr 15, 2016 at 18:59
  • Fixed it to writerow. Thanks. Commented Apr 15, 2016 at 19:01
  • @ThomasChristensen how would I code it so that I write to a new file each time...yet the file name stays "constant" so that the loop would work properly if it needed to cycle through a few times? Commented Apr 15, 2016 at 19:04
  • You need to use a second file. You can create a new file and write to it. When the write has completed successfully, you would then rename the temporary file to the original name. Commented Apr 15, 2016 at 19:07

1 Answer 1

0

Just a way to do it for inspiration. There is no need to make a new file and rename in this case:

import csv
import os
import random

def ensure_csv_max_size(size):
    # Read all data into memory.
    with open('CSVfile', 'r') as f:
        reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE)
        csv_data = list(reader)

    # Remove first row, save, check repeatedly.
    for i in range(len(csv_data)):
        with open('CSVfile', 'w') as f:
            writer = csv.writer(f, delimiter=',', quoting=csv.QUOTE_NONE)
            writer.writerows(csv_data[i:])

        print(os.path.getsize('CSVfile'))
        if os.path.getsize('CSVfile') <= size:
            break

def write_csv(datalist):   

    with open("CSVfile",'a') as f:
        writer = csv.writer(f, delimiter=',' ,quoting=csv.QUOTE_NONE)
        writer.writerow(datalist)

    while os.path.getsize("CSVfile") > 100000:
        print("cut")
        ensure_csv_max_size(100000)

for i in range(1000):
    write_csv([random.randint(0, 100) for _ in range(10)])
Sign up to request clarification or add additional context in comments.

4 Comments

And if the system crashes you've lost the file; and in addition this will ruin the sd card in no time.
@AnttiHaapala All true.
@totoro thank you for the example. I ended up doing as everyone suggested and writing a new file then renaming it to the original one. Didn't seem like the risk of corrupting things was worth it.
@Crewski Good idea. My answer was merely providing a generel example. I didn't have Raspberry Pi in mind.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.