import tempfile import csv import os for max_size in range(1000, 11000,9999): # tmp_file = tempfile.TemporaryFile("w+", newline="") tmp_file = tempfile.SpooledTemporaryFile(max_size=max_size, mode="w+", newline="") csv_writer = csv.writer(tmp_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_ALL) csv_reader = csv.reader(tmp_file) for i in range(100): csv_writer.writerow([f"Line {i}", "Value 1", "Value 2", 2001, "A space odyssey"]) if tmp_file._rolled: rolled_str = "rolled over" else: rolled_str = "not rolled over" file_size=tmp_file.tell() tmp_file.seek(0) print("\nseek(0) went to start of", file_size,"byte file, and this is the first line: ", tmp_file.readline()) try: print(f"PASSED: With SpooledTemporaryFile (max_size={max_size}), which was {rolled_str}, am ABLE to csv read the second line in the file: ", next(csv_reader)) except StopIteration as exception: print(f"FAILED: With SpooledTemporaryFile (max_size={max_size}), which was {rolled_str}, am UNABLE to csv read the second line in the file.")