Writing to a file

G

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

 
C

cassiope

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



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

John Gordon

In said:
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.)
 
G

Gregory Ewing

John said:
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.
 
G

Gregory Ewing

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

Steven D'Aprano

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top