473,670 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(parame ter):
'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 1281
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(parame ter):
* * '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(parame ter):
* * 'Lots of code followed by:
* * with open(summaryFn, 'ab') as f:
* * * * writer = csv.writer(f)
* * * * writer.writerow (header)
def simulate(parame ter):
'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(parame ter):
'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***********@A cm.Org

Sep 24 '08 #5

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

Similar topics

5
12550
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 file may have been created but a crash occurred at that point in time (while it was being created), damaging the file. Now, my program which needs to read from this file, should first check that it's in good condition, and that it hasn't been...
2
3031
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 they successfuly download. My problem revolves around detecting a successful download, the steps I take to handle the download are as follows:
5
8752
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 work. I am sure that this probably fairly simple, but I have not been able to figure it out. Z.K.
4
3079
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6216
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.