Writing to a file

  • Follow


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
>>> 

0
Reply jyoung79 3/25/2011 3:07:19 PM

On Mar 25, 8:07=A0am, <jyoun...@kc.rr.com> wrote:
> Just curious how others view the 2 examples below for creating and
> writing to a file in Python (in OS X). =A0Is one way better than the othe=
r? =A0
> If it was a large amount of text, would the 'os.system' call be a bad
> way to do it?
>
> Thanks.
>
> Jay
>
>
>
> >>> f =3D 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...
0
Reply cassiope 3/25/2011 3:25:58 PM


In <mailman.1249.1301065649.1189.python-list@python.org> <jyoung79@kc.rr.com> 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
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

0
Reply John 3/25/2011 4:02:04 PM

On Friday, March 25, 2011 11:07:19 AM UTC-4, jyou...@kc.rr.com wrote:
>
> >>> f = open('~/Desktop/test.txt', 'w')
> >>> f.write('testing 1... 2... 3...')
> >>> f.close()

Consider using "with" to automatically close the file and os.path for cross-platform compatibility:

    import os.path
    user_home = os.path.expanduser('~')
    test_absname = os.path.join(user_home, 'Desktop', 'test.txt')
    
    with open(test_absname, 'w') as test:
        test.write('testing 1... 2... 3...')

0
Reply eryksun 3/26/2011 1:11:23 AM

On Friday, March 25, 2011 11:07:19 AM UTC-4, jyou...@kc.rr.com wrote:
>
> >>> f = open('~/Desktop/test.txt', 'w')
> >>> f.write('testing 1... 2... 3...')
> >>> f.close()

Consider using "with" to automatically close the file and os.path for cross-platform compatibility:

    import os.path
    user_home = os.path.expanduser('~')
    test_absname = os.path.join(user_home, 'Desktop', 'test.txt')
    
    with open(test_absname, 'w') as test:
        test.write('testing 1... 2... 3...')

0
Reply eryksun 3/26/2011 1:11:23 AM

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
0
Reply Gregory 3/26/2011 1:45:28 AM

jyoung79@kc.rr.com 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
0
Reply Gregory 3/26/2011 1:49:31 AM

On Sat, 26 Mar 2011 14:49:31 +1300, Gregory Ewing wrote:

> jyoung79@kc.rr.com 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
0
Reply Steven 3/26/2011 11:51:50 AM

7 Replies
304 Views

(page loaded in 0.094 seconds)

Similiar Articles:













7/22/2012 5:05:03 PM


Reply: