<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"><<a href="mailto:sander.sweers@gmail.com">sander.sweers@gmail.com</a>></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 = "C:\\testfile.txt"<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'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('please enter filename: ')<br> if os.path.exists(fname):<br> print('ERROR: %s exists!') % fname<br> elif fname.isspace():<br> print('Space is not allowed!')<br>
else:<br> print('you entered %s') % fname<br> break<br><br>all = [] # container list to hold user input lines<br>print "\nPopulate your file! \n"<br>quit_prompt = "[to quit enter a dot '.' by itself]--> "<br>
<br>while True:<br> entry = raw_input(quit_prompt)<br> if entry == '.':<br> break<br> else:<br> all.append(entry)<br><br>confirm = raw_input('save file to disk?(y/N)')<br>confirm = confirm.lower() #convert to lowercase<br>
if confirm == 'y':<br> fobj = open(fname, 'w')<br> fobj.write('\n'.join(all))<br> fobj.close()<br> print('DONE! open %s to view your file') % fname<br>else:<br> print 'not saving to disk!'<br>
sys.exit()<br><br>Thank you.<br><br><br></div></div>-- <br>Best Regards,<br>bibimidi<br><br><br>
</div>