Detecting the first time I open/append to a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tkpmep@hotmail.com

    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
  • Sean Davis

    #2
    Re: Detecting the first time I open/append to a file

    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

    Comment

    • Arnaud Delobelle

      #3
      Re: Detecting the first time I open/append to a file

      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

      Comment

      • Terry Reedy

        #4
        Re: Detecting the first time I open/append to a file

        tkpmep@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?

        Comment

        • Scott David Daniels

          #5
          Re: Detecting the first time I open/append to a file

          Terry Reedy wrote:
          tkpmep@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
          Scott.Daniels@A cm.Org

          Comment

          Working...