<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
For Python 3, the syntax is similar:<br>
<pre>>>> with open('test.txt','w') as f:
... print('Text',file=f)
...
>>> with open('test.txt') as f:
... print(f.read(), end='')
...
Text
>>>
</pre>
You can also do this in Python >=2.6 (or ipython) if you start with<br>
<pre>from __future__ import print_function
</pre>
On 07/15/2010 05:45 PM, Max Bane wrote:
<blockquote
cite="mid:AANLkTilfW4c3aiwmGRkxhopM2QbXzSaSiPAW8OQ2xyYA@mail.gmail.com"
type="cite">
<meta http-equiv="Context-Type"
content="text/html; charset=ISO-8859-1">
The standard Python print statement supports something like this (not
sure about Python 3.0, though).
<div>
<div><br>
</div>
<div>In [1]: f = open('test.txt', 'w')</div>
<div><br>
</div>
<div>In [2]: print >> f, 'hello, world'</div>
<div><br>
</div>
<div>In [3]: f.close()</div>
<div><br>
</div>
<div>In [4]: !cat test.txt</div>
<div>hello, world</div>
<div><br>
</div>
<br>
<div>On Thu, Jul 15, 2010 at 6:00 AM, Richard Parsons <span><<a
moz-do-not-send="true" href="mailto:richard.lee.parsons@gmail.com">richard.lee.parsons@gmail.com</a>></span>
wrote:<br>
<blockquote>How do I pipe the output into a file?<br>
<br>
I want to do this, but it doesn't work...<br>
<br>
print variable > some_filename<br>
<br>
Thanks,<br>
Richard<br>
_______________________________________________<br>
IPython-User mailing list<br>
<a moz-do-not-send="true" href="mailto:IPython-User@scipy.org">IPython-User@scipy.org</a><br>
<a moz-do-not-send="true"
href="http://mail.scipy.org/mailman/listinfo/ipython-user">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
</blockquote>
</div>
<br>
<br>
<br>
-- <br>
Max Bane<br>
PhD Student, Linguistics<br>
University of Chicago<br>
<a moz-do-not-send="true" href="mailto:bane@uchicago.edu">bane@uchicago.edu</a><br>
</div>
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
IPython-User mailing list
<a class="moz-txt-link-abbreviated" href="mailto:IPython-User@scipy.org">IPython-User@scipy.org</a>
<a class="moz-txt-link-freetext" href="http://mail.scipy.org/mailman/listinfo/ipython-user">http://mail.scipy.org/mailman/listinfo/ipython-user</a>
</pre>
</blockquote>
<br>
</body>
</html>