B
Bruno Desthuilliers
Sean Davis a écrit :
s/file handle/file object/
This being said, I can only second Sean's advice. And anyway, computing
something and writing the result(s) of this computation to the file are
orthogonal and mostly unrelated responsabilities, so they shouldn't be
handled by the same function.
Here's a possible reorganisation of your code:
def simulate(parameter):
# lots of code followed by
return sigma
def main():
# presumably some other code here
with open(summaryFn, 'ab') as f:
writer = csv.writer(f)
writer.writerow(header)
for param in range(10):
writer.writerow(simulate(param))
You could use os.path.exists() to check if the file is there.
However, the file could have been left over from a previous execution,
etc. What might make sense is to open the file only once, store the
file handle, and write to that throughout the execution.
s/file handle/file object/
This being said, I can only second Sean's advice. And anyway, computing
something and writing the result(s) of this computation to the file are
orthogonal and mostly unrelated responsabilities, so they shouldn't be
handled by the same function.
Here's a possible reorganisation of your code:
def simulate(parameter):
# lots of code followed by
return sigma
def main():
# presumably some other code here
with open(summaryFn, 'ab') as f:
writer = csv.writer(f)
writer.writerow(header)
for param in range(10):
writer.writerow(simulate(param))