If the file needs to be emptied once per program run, but appended multiple times within a run, you could always just use a global (or class member state) to ensure it's only opened once.
import atexit
csvfile = None
def generateCSVfile(fileName,fileDescription,fileLocation,md5Hash):
global csvfile
if csvfile is None:
# Lazily open file on first call
csvfile = open('deploymentTemplate.csv', 'w')
atexit.atexit(csvfile.close) # Close cleanly on program exit
try:
csvwriter = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL, newline='')
# do whatever writing you need to csvwriter
finally:
csvfile.flush() # Match behavior of repeated with/open, force predictable flush
If there might be multiple CSV files involved, you might use a class with instance state and a method to do the writing, so each file can be independently cleared once and appended many times. In this case, due to limits on the number of open file handles, reopening for append on each use is slower but safer than opening once and leaving open. You can use caching so the class is a singleton for any given file name too:
import weakref
class CSVGenerator:
CACHE = {}
CACHELOCK = threading.Lock()
def __new__(cls, csvfilename):
canonicalname = os.path.realpath(csvfilename)
newself = super().__new__(cls)
with cls.CACHELOCK:
self = cls.CACHE.setdefault(canonicalname, newself)
if newself is self:
# First time we opened this file, clear file and initialize instance
with open(canonicalname, 'w') as f:
pass
self.csvfilename = canonicalname
self.appendlock = threading.Lock()
return self
def generateCSVfile(self, fileName, fileDescription, fileLocation, md5Hash):
with newself.appendlock, open(self.csvfilename, 'a', newline='') as csvfile:
createRow = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
# Perform writes to file
Usage of the class can be either:
CSVGenerator(somecsvfilename).generateCSVfile(...args...)
which acquires an instance briefly (creating it if needed) then writes once, or it can create and store an instance and reuse it (saves the overhead of cache lookup, but functionally identical).