Neat syntax!  Still doesn&#39;t account for the closing of a non-named file argument, though.<br><br>This is uncanny timing, as I just ran into this issue today and came up with this:<br><br><div style="margin-left: 40px; font-family: courier new,monospace;">
def foo(f_obj_or_name):<br>    if isinstance(f_obj_or_name, str):<br>        with open(f_obj_or_name) as f_obj:<br>            return foo(f_obj)<br><br>    [...] # do file processing<br></div><br>My problem actually involved both and input and output file... ugh, doubled the above code<br>
<br>Cheers!<br>  - Jeremy<br><br><br>--<br>Jeremy R Fishman<br><a href="mailto:jeremy.r.fishman@gmail.com">jeremy.r.fishman@gmail.com</a><br><a href="http://jfishman.org">http://jfishman.org</a><br><br><div class="gmail_quote">
On Tue, Oct 27, 2009 at 11:29 AM, Aahz <span dir="ltr">&lt;<a href="mailto:aahz@pythoncraft.com">aahz@pythoncraft.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Tue, Oct 27, 2009, Andrew Dalke wrote:<br>
&gt;<br>
&gt; def process(file_obj_or_name):<br>
&gt;     with isinstance(file_obj_or_name, str) and \<br>
&gt;             open(file_obj_or_name) or file_obj_or_name as file_obj:<br>
&gt;         print &quot;First line&quot;, repr(file_obj.readline())<br>
&gt;     sys.stdout.write(&quot;Finished with %r\n&quot; % (file_obj_or_name,))<br>
<br>
</div>Anyone using ``with`` ought to use the real ternary (reformatted to my<br>
preference ;-):<br>
<div class="im"><br>
     with (<br>
            open(file_obj_or_name)<br>
            if isinstance(file_obj_or_name, str)<br>
</div>            else file_obj_or_name<br>
            ) as file_obj:<br>
<font color="#888888">--<br>
Aahz (<a href="mailto:aahz@pythoncraft.com">aahz@pythoncraft.com</a>)           &lt;*&gt;         <a href="http://www.pythoncraft.com/" target="_blank">http://www.pythoncraft.com/</a><br>
<br>
&quot;You could make Eskimos emigrate to the Sahara by vigorously arguing --<br>
at hundreds of screens&#39; length -- for the wonder, beauty, and utility of<br>
snow.&quot;  --PNH to rb in r.a.sf.f<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>