<div dir="ltr">Thanks to all of you that replied. Your inputs are very helpful and informative.<br><br><br><div class="gmail_quote">On Tue, Dec 1, 2009 at 12:14 AM, Sander Sweers <span dir="ltr">&lt;<a href="mailto:sander.sweers@gmail.com">sander.sweers@gmail.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;"><br>
There is no exception to alert you a file already exists. Depending on<br>
how you open the file (mode) it will either read, write or append to<br>
the file. If the file exists and you open it in write mode it *will*<br>
overwrite the file. See [1] for more info.<br>
<br>
So what you need to do is check yourself if the file exists and make<br>
the python script take appriate action. For example:<br>
<br>
import os<br>
fname = &quot;C:\\testfile.txt&quot;<br>
if os.path.exists(fname):<br>
    do something if exists<br>
else:<br>
   do something else if not exists<br>
<br>
Greets<br>
Sander<br></blockquote><div><br>Like i thought so, there is no exception to catch if a file already exist. I&#39;ve been browsing the many types of exceptions and cant find anything thats close. Thank you for clarifying.<br>
<br>I may have misunderstood the exercise question and will have to return to it to do another approach.<br><br>For the record below is the working copy of the script. Here i understand how the os.path.exists() method works.<br>
<br>while True:<br>    fname = raw_input(&#39;please enter filename: &#39;)<br>    if os.path.exists(fname):<br>        print(&#39;ERROR: %s exists!&#39;) % fname<br>    elif fname.isspace():<br>        print(&#39;Space is not allowed!&#39;)<br>
    else:<br>        print(&#39;you entered %s&#39;) % fname<br>        break<br><br>all = []        # container list to hold user input lines<br>print &quot;\nPopulate your file! \n&quot;<br>quit_prompt = &quot;[to quit enter a dot &#39;.&#39; by itself]--&gt; &quot;<br>
<br>while True:<br>    entry = raw_input(quit_prompt)<br>    if entry == &#39;.&#39;:<br>        break<br>    else:<br>        all.append(entry)<br><br>confirm = raw_input(&#39;save file to disk?(y/N)&#39;)<br>confirm = confirm.lower()   #convert to lowercase<br>
if confirm == &#39;y&#39;:<br>    fobj = open(fname, &#39;w&#39;)<br>    fobj.write(&#39;\n&#39;.join(all))<br>    fobj.close()<br>    print(&#39;DONE! open %s to view your file&#39;) % fname<br>else:<br>    print &#39;not saving to disk!&#39;<br>
    sys.exit()<br><br>Thank you.<br><br><br></div></div>-- <br>Best Regards,<br>bibimidi<br><br><br>
</div>