Printing current time to a file

Discussion in 'Python' started by zolaris@gmail.com, Nov 11, 2005.

  1. Guest

    I am trying to print the current system time to a file. I know only a
    little bit about Python. I have gotten the very simple:

    Print time.time()

    to work properly. From what I gather the line to print it to a file
    should look like:

    self.log.write(time.ctime(time.time()))

    But that prints nothing in the file assigned to log. Is there
    something I should be doing extra?

    Thanks.
    , Nov 11, 2005
    #1
    1. Advertising

  2. Guest

    zolaris> self.log.write(time.ctime(time.time()))

    zolaris> But that prints nothing in the file assigned to log. Is there
    zolaris> something I should be doing extra?

    There's no newline in there. You probably need to flush the file:

    self.log.write(time.ctime(time.time()))
    self.log.flush()

    You might want to write a newline after the time as well. If the logfile is
    line-buffered that will also provoke a flush.

    Skip
    , Nov 11, 2005
    #2
    1. Advertising

  3. Peter Hansen Guest

    wrote:
    > I am trying to print the current system time to a file. I know only a
    > little bit about Python. I have gotten the very simple:
    >
    > Print time.time()
    >
    > to work properly. From what I gather the line to print it to a file
    > should look like:
    >
    > self.log.write(time.ctime(time.time()))
    >
    > But that prints nothing in the file assigned to log. Is there
    > something I should be doing extra?


    >>> import time
    >>> time.ctime(time.time())

    'Thu Nov 10 22:00:57 2005'

    Since that clearly returns a string, and the write() method takes a
    string, you either do NOT have a "file assigned to log", or you haven't
    opened it, or perhaps you are expecting to see the output even though
    you haven't closed the file or flushed it.

    Maybe showing us the missing code would help...

    -Peter
    Peter Hansen, Nov 11, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manu
    Replies:
    1
    Views:
    6,078
    Natty Gur
    Jul 20, 2003
  2. B. Schad
    Replies:
    8
    Views:
    853
    B. Schad
    Sep 4, 2003
  3. The One
    Replies:
    5
    Views:
    557
    Steve W. Jackson
    Nov 29, 2006
  4. flamesrock
    Replies:
    8
    Views:
    435
    Hendrik van Rooyen
    Nov 24, 2006
  5. Jack
    Replies:
    3
    Views:
    582
    Tad J McClellan
    Dec 22, 2008
Loading...

Share This Page