Detecting the first time I open/append to a file

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

Sean Davis a écrit :
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))
 
T

tkpmep

I have a simulation that runs many times with different parameters,
and I want to aggregate the output into a single file with one rub: I
want a header to be written only the first time. My program looks a
bit like this:

def main():
for param in range(10):
simulate(param)

def simulate(parameter):
'Lots of code followed by:
with open(summaryFn, 'ab') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerow(Sigma)


If I can sense that the file is being created in the first iteration,
I can then use an if statement to decide whether or not I need to
write the header. Question: how can I tell if the file is being
created or if this its the first iteration? It's unrealistic to test
the value of the parameter as in the real problem, there are many
nested loops in main, and the bounds on the loop indices may change.

Thanks in advance for your assistance

Thomas Philips
 
S

Sean Davis

I have a simulation that runs many times with different parameters,
and I want to aggregate the output into a  single file with one rub: I
want a header to be written only the first time. My program looks a
bit like this:

def main():
    for param in range(10):
        simulate(param)

def simulate(parameter):
    'Lots of code followed by:
    with open(summaryFn, 'ab') as f:
        writer = csv.writer(f)
        writer.writerow(header)
        writer.writerow(Sigma)

If I can sense that the file is being created in the first iteration,
I can then use an if statement to decide whether or not I need to
write the header. Question: how can I tell if the file is being
created or if this its the first iteration? It's unrealistic to test
the value of the parameter as in the real problem, there are many
nested loops in main, and the bounds on the loop indices may change.

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.

Sean
 
A

Arnaud Delobelle

I have a simulation that runs many times with different parameters,
and I want to aggregate the output into a  single file with one rub: I
want a header to be written only the first time. My program looks a
bit like this:

def main():
    for param in range(10):
        simulate(param)

def simulate(parameter):
    'Lots of code followed by:
    with open(summaryFn, 'ab') as f:
        writer = csv.writer(f)
        writer.writerow(header)

def simulate(parameter):
'Lots of code followed by:

with open(summaryFn, 'ab') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerow(Sigma)
        writer.writerow(Sigma)

If I can sense that the file is being created in the first iteration,
I can then use an if statement to decide whether or not I need to
write the header. Question: how can I tell if the file is being
created or if this its the first iteration? It's unrealistic to test
the value of the parameter as in the real problem, there are many
nested loops in main, and the bounds on the loop indices may change.

Thanks in advance for your assistance

Thomas  Philips

You can use he os.path module:

appending = os.path.exists(summaryFn)
with open(summaryFn, 'ab') as f:
writer = ...
if not appending:
writer.writerow(header)
writer.writerow(Sigma)

But if you only write to the file in one code location, you can set a
flag.

HTH
 
T

Terry Reedy

I have a simulation that runs many times with different parameters,
and I want to aggregate the output into a single file with one rub: I
want a header to be written only the first time. My program looks a
bit like this:

def main():
for param in range(10):
simulate(param)

def simulate(parameter):
'Lots of code followed by:
with open(summaryFn, 'ab') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerow(Sigma)


If I can sense that the file is being created in the first iteration,
I can then use an if statement to decide whether or not I need to
write the header. Question: how can I tell if the file is being
created or if this its the first iteration? It's unrealistic to test
the value of the parameter as in the real problem, there are many
nested loops in main, and the bounds on the loop indices may change.

How about file.tell == 0? or have I misunderstood the requirement?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top