Writing to a file

Discussion in 'Python' started by Guest, Mar 25, 2011.

  1. Guest

    Guest Guest

    Just curious how others view the 2 examples below for creating and
    writing to a file in Python (in OS X). Is one way better than the other?
    If it was a large amount of text, would the 'os.system' call be a bad
    way to do it?

    Thanks.

    Jay


    >>> f = open('~/Desktop/test.txt', 'w')
    >>> f.write('testing 1... 2... 3...')
    >>> f.close()
    >>>
    >>>
    >>> import os
    >>> os.system('echo "Testing a... b... c..." > "~/Desktop/test2.txt"')

    0
    >>>
     
    Guest, Mar 25, 2011
    #1
    1. Advertising

  2. Guest

    cassiope Guest

    On Mar 25, 8:07 am, <> wrote:
    > Just curious how others view the 2 examples below for creating and
    > writing to a file in Python (in OS X).  Is one way better than the other?  
    > If it was a large amount of text, would the 'os.system' call be a bad
    > way to do it?
    >
    > Thanks.
    >
    > Jay
    >
    >
    >
    > >>> f = open('~/Desktop/test.txt', 'w')
    > >>> f.write('testing 1... 2... 3...')
    > >>> f.close()

    >
    > >>> import os
    > >>> os.system('echo "Testing a... b... c..." > "~/Desktop/test2.txt"')

    > 0
    >
    >


    I personally consider each use of os.system(..) as something that
    needs to be eliminated.
    Maybe 'echo' isn't too bad... but (for example) is it subject to
    limited argument lengths?
    Does it perform differently on different OSs? And if it's not
    something intrinsic to the
    OS, might there be 'PATH' issues: where is the called program?
    Finally, there may be
    some security issues (in general) though perhaps not in your specific
    example.

    Of course, if speed is a real issue there may be some value in
    buffering a long string
    before using whatever method to save it in a file. OTOH these
    functions usually include
    system buffering (which the incremental os.system(..) call clearly
    won't have).

    Hope that helps...
     
    cassiope, Mar 25, 2011
    #2
    1. Advertising

  3. Guest

    John Gordon Guest

    In <> <> writes:

    > Just curious how others view the 2 examples below for creating and
    > writing to a file in Python (in OS X). Is one way better than the other?
    > If it was a large amount of text, would the 'os.system' call be a bad
    > way to do it?


    The write() way is much better. (However, I'm not sure it will do what
    you were expecting with the tilde in the file path.)

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Mar 25, 2011
    #3
  4. John Gordon wrote:

    > The write() way is much better. (However, I'm not sure it will do what
    > you were expecting with the tilde in the file path.)


    It won't, but the os.path.expanduser() function can
    be used to fix that.

    --
    Greg
     
    Gregory Ewing, Mar 26, 2011
    #4
  5. wrote:

    >>>>import os
    >>>>os.system('echo "Testing a... b... c..." > "~/Desktop/test2.txt"')


    This is like going out the back door, getting a ladder out of
    the shed and climbing through your bedroom window to get into
    bed at night, instead of just using the stairs.

    Use open/write/close. It's much more direct and efficient.

    --
    Greg
     
    Gregory Ewing, Mar 26, 2011
    #5
  6. On Sat, 26 Mar 2011 14:49:31 +1300, Gregory Ewing wrote:

    > wrote:
    >
    >>>>>import os
    >>>>>os.system('echo "Testing a... b... c..." > "~/Desktop/test2.txt"')

    >
    > This is like going out the back door, getting a ladder out of the shed
    > and climbing through your bedroom window to get into bed at night,
    > instead of just using the stairs.
    >
    > Use open/write/close. It's much more direct and efficient.



    I would say the analogy is more like calling the local handyman to come
    to your house and get the ladder for you.



    --
    Steven
     
    Steven D'Aprano, Mar 26, 2011
    #6
    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. Mark Kamoski
    Replies:
    1
    Views:
    2,453
  2. BLF
    Replies:
    4
    Views:
    1,282
    Mike Treseler
    Aug 7, 2004
  3. John Carnahan

    File Access error - writing to .txt file

    John Carnahan, Jul 18, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    805
    John Carnahan
    Jul 18, 2003
  4. JuneCleaver

    Writing to a log file with in an aspx file.

    JuneCleaver, Apr 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,048
    Juan Romero
    Apr 8, 2004
  5. HNguyen
    Replies:
    4
    Views:
    2,408
    HNguyen
    Dec 21, 2004
Loading...

Share This Page