'Why is the CSV file not being emptied before rewriting even after calling `file.truncate()`?
I'm writing a program that reads from a database and puts the contents into a .txt document. Because I don't want this .txt to be huge, I delete all the contents with the file.truncate() method before rewriting the rows. However, the file just grows larger and larger each time I run the script.
import rethinkdb as rt
import csv
import datetime as dt
import time
#EXAMPLE LOCAL IP FOR THE SAKE OF POSTING
rt.connect("0.0.0.0").repl()
date = dt.datetime.now()
updates = {'Object A':0,'Object B':0}
while True:
for code in updates:
prev = {}
query = list(rt.db("test").table("table").filter(rt.row["Name"][:len(code)+1] == code + ' ').run())
try:
# IF TEXT FILE EXISTED ALREADY, READ THE FILE
with open(code + '{:02d}'.format(date.year) + '{:02d}'.format(date.month) + '{:02d}'.format(date.day) + '.txt',"r+",newline='') as file:
reader = csv.reader(file, delimiter='\t')
writer = csv.writer(file, delimiter='\t')
print('prev len1: '+ str(len(prev)))
prev = {row[0]:row[1] for row in reader}
file.truncate()
for item in query:
try:
if item['Counter'] < prev[item['Name']]: # THE KEYERROR THAT THE TRY BLOCK IS TRYING TO CATCH WOULD BE RAISED IN THIS LINE
prev[item['Name']] = prev[item['Name']] + item['Counter']
else:
prev[item['Name']] = item['Counter']
except KeyError:
prev[item['Name']] = item['Counter']
writer.writerow([item['Name'],prev[item['Name']]])
except:
# IF TEXT FILE FOR THE DAY DOESN'T EXIST, CREATE A NEW FILE
with open(code + '{:02d}'.format(date.year) + '{:02d}'.format(date.month) + '{:02d}'.format(date.day) + '.txt',"w",newline='') as file:
for item in query:
writer = csv.writer(file,delimiter='\t')
writer.writerow([item['Name'],item['Counter']])
file.close()
print('Updated...')
time.sleep(30)
Solution 1:[1]
file.truncate according the manual does this:
Truncate the file’s size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position…
So you need to supply argument size=0:
file.truncate(size=0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
