Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Writing to a file

Reply
Thread Tools

Writing to a file

 
 
Guest
Posts: n/a
 
      03-25-2011
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
>>>


 
Reply With Quote
 
 
 
 
cassiope
Guest
Posts: n/a
 
      03-25-2011
On Mar 25, 8:07*am, <(E-Mail Removed)> 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...
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      03-25-2011
In <(E-Mail Removed)> <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Gregory Ewing
Guest
Posts: n/a
 
      03-26-2011
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
 
Reply With Quote
 
Gregory Ewing
Guest
Posts: n/a
 
      03-26-2011
(E-Mail Removed) 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-26-2011
On Sat, 26 Mar 2011 14:49:31 +1300, Gregory Ewing wrote:

> (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Question: Writing text file based TestBenches vs. Waveform file based simulation. BLF VHDL 4 08-07-2004 12:44 AM
File Access error - writing to .txt file John Carnahan ASP .Net 2 07-18-2003 10:35 PM
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY? Mark Kamoski ASP .Net 1 07-04-2003 12:02 PM



Advertisments