473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Detecting the first time I open/append to a file

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
Sep 23 '08 #1
4 1269
On Sep 23, 2:02*pm, tkp...@hotmail.com wrote:
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
Sep 23 '08 #2
On Sep 23, 7:02*pm, tkp...@hotmail.com wrote:
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

--
Arnaud

Sep 23 '08 #3
tk****@hotmail.com wrote:
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?

Sep 23 '08 #4
Terry Reedy wrote:
tk****@hotmail.com wrote:
>...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? ...

How about file.tell == 0? or have I misunderstood the requirement?
OI thought roughly the same thing when I saw this,
how about
if not file.tell():
<produce header>

--Scott David Daniels
Sc***********@Acm.Org

Sep 24 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jole | last post by:
Hi I'm writing a program that needs to read from a file. In order for the program to be robust, it should somehow check that the file isn't corrupt, or stuffed in any way. For example, that...
2
by: Sam-Kiwi | last post by:
I've spent the last 6 months developing a pay-per-download website using ASP.NET Users purchase documents and then download them. The intention is that users are only charged for documents...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.