Is this what you are looking for?<br><br>#!/usr/bin/python<br>&#39;makeTextFile.py -- create text file&#39;<br><br>import os<br><br># get filename<br>#while True:<br>#&nbsp;&nbsp; fname = raw_input(&#39;Enter file name: &#39;)<br>#&nbsp;&nbsp; if 
os.path.exists(fname):<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&quot;*** ERROR: &#39;%s&#39; already exists&quot; % fname<br>#&nbsp;&nbsp; else:<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break<br><br>while True:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fname = raw_input(&#39;Enter file name: &#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fobj = open(fname, &#39;r&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break<br><br># get file content (text) lines<br>all = []<br>print &quot;\nEnter lines (&#39;.&#39; by itself to quit).\n&quot;<br><br># loop until user terminates input
<br>while True:<br>&nbsp;&nbsp; entry = raw_input(&#39;&gt; &#39;)<br>&nbsp;&nbsp; if entry == &#39;.&#39;:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break<br>&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; all.append(entry)<br><br># write lines to file with NEWLINE line terminator<br>fobj = open(fname, &#39;w&#39;)
<br>fobj.write(&#39;\n&#39;.join(all))<br>fobj.close()<br>print &#39;DONE!&#39;<br clear="all"><br>-- <br>Arvind Deshpande
<br><br><div><span class="gmail_quote">On 9/8/07, <b class="gmail_sendername">Alan Gauld</b> &lt;<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&quot;Christopher Spears&quot; &lt;<a href="mailto:cspears2002@yahoo.com">cspears2002@yahoo.com</a>&gt; wrote<br><br>&gt; I have been asked to replace this while loop with a<br>&gt; try and except clause:<br>&gt;<br>&gt; while True:
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;fname = raw_input(&#39;Enter file name: &#39;)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;if os.path.exists(fname):<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&quot;*** ERROR: &#39;%s&#39; already exists&quot; % fname<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;else:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break<br>&gt;<br>
&gt; I&#39;m not sure how to do this.&nbsp;&nbsp;I looked at the back of<br>&gt; the book, and I don&#39;t see an exception that is raised<br>&gt; when a previously existing file is found.&nbsp;&nbsp;Any hints?<br><br>The loop simply detects if the file exists *or not*
<br>If the file does not exist you exit the loop.<br>Can you find a way using try/except to detect<br>if the file does not exist?<br><br>That will replace the body of the while loop,<br>I can&#39;t think of any way to replace the loop itself
<br>with try./except...<br><br>And I agree this is not an obvious place to use<br>try/except. Your earlier example is more typical.<br><br>--<br>Alan Gauld<br>Author of the Learn to Program web site<br><a href="http://www.freenetpages.co.uk/hp/alan.gauld">
http://www.freenetpages.co.uk/hp/alan.gauld</a><br><br>_______________________________________________<br>Tutor maillist&nbsp;&nbsp;-&nbsp;&nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">
http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>