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: Writing to a file - comp.lang.pythonJust 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 i... Reading from and writing to a text file in verilog hdl - comp.arch ...hi I know $memreadh, $memreadb are used to read data from a text file. I have used a memory file for reading data. reg [0:7] mem[1:500]; initial $m... How to write to file within awk ? - comp.lang.awkOn 17.12.2010 05:38, moonhkt wrote: > Hi All > > How to write to file within awk ? > > I want output to two file ? Quote your file names. > > awk 'BEGIN {FS ... Writing a matrix with header into a CSV/XLS file - comp.soft-sys ...Hi there! I have a numerical matrix A, that I would like to write into a CSV or XLS file. I would like each column to have a header, so I defined t... error writing to new file - comp.emacsDon't know what I did wrong, but suddenly I get an error when I try to write on a newly created file.: byte-code: Wrong type argument: stringp, # I ... Write results to text file??? - comp.soft-sys.matlabI know the general way to write to a text file but now I want it in a certain format Say i have two numbers that I want to get reported (a and b) ... write a cell array to text file.. - comp.soft-sys.matlab ...Hi, I have a cell array 'a' with k rows and 5 columns and i want to write this in a text file with the same structure. Each row is for example: a={... How to write testbench file? - comp.lang.vhdlHi, i am using an actel fpga to generate pwm signal. There are these IP cores in the libero software for you to drage and drop into your design. I h... Writing a outlook msg file using java - comp.lang.java.programmer ...Hi, I am writing a code that reads data (in bytes) from URL stream and writes the bytes to a BufferedInputStream (msg file). When i compare the CONTEN... write texture to file - comp.graphics.api.openglHello World, I was rendering to textures and should now write these textures to a file for being able to read in later again. Does somebody has code ... How to: Write Text to a File - Microsoft Corporation: Software ...The following example shows how to write text to a text file. It reads all the text files from the user's My Documents folder by using a "*.txt" search pattern, and ... C# Station: Reading and Writing Text FilesLearn how to read from and write to text files with C# (C Sharp). 7/22/2012 5:05:03 PM
|