Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-07-2010, 09:35 PM   PM User | #1
pippin418
New Coder

 
Join Date: Dec 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
pippin418 is an unknown quantity at this point
Process different commands based on error

Okay, I have this:
Code:
if com == 'del':
        rem = raw_input("What file: ")
        rea = raw_input("Really remove %s? (y/n)" % rem)
        if rea in ('y'):
                try:
                        os.remove(rem)
                except OSError:
                        print "That is a directory, del deletes files. Use ddel instead."
                        raw_input("Press ENTER to return.")
                        goto .home
                else:
                        goto .home
        elif rea in ('n'):
                goto .home
Now, don't worry about the goto, I have a module installed for it, and I know you're not supposed to use it... I'm using it on my computer though. Anyway.

In that script you should see the except OSError:, that works great, but there's more than one type of error you can get with it. How can I run different commands based on the different errors? (All of them will be OSErrors)\

Thanks

Last edited by pippin418; 05-08-2010 at 12:10 AM..
pippin418 is offline   Reply With Quote
Old 05-08-2010, 08:04 PM   PM User | #2
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
More stuff here: http://docs.python.org/tutorial/errors.html

For your problem:
Code:
some_file_name = "whatever"

try:
    os.remove(some_file_name)

except OSError as (errno, strerror):
    # Decide what to do based on error code:
    # File doesn't exist.
    if errno == 2:
        print "Do something..."
    # Trying to delete a directory.
    elif errno == 21:
        print "Do something else..."
    # elif errno = ??: do something else....

else:
    print "LOLWUT?"
__________________
I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
** Independent Multimedia Artist in Pasig **
Samhain13 is offline   Reply With Quote
Old 05-09-2010, 06:49 AM   PM User | #3
pippin418
New Coder

 
Join Date: Dec 2009
Posts: 40
Thanks: 4
Thanked 0 Times in 0 Posts
pippin418 is an unknown quantity at this point
Thanks a lot.

Last edited by pippin418; 05-09-2010 at 07:01 AM..
pippin418 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:02 AM.


Advertisement
Log in to turn off these ads.