How do I continue after the error?

Discussion in 'Python' started by chad, Jun 5, 2009.

  1. chad

    chad Guest

    Let's say I have a list of 5 files. Now lets say that one of the files
    reads nude333.txt instead of nude3.txt. When this happens, the program
    generates an error and quits. What I want it to do is just skip over
    the bad file and continue on with the next one. Below is the actual
    code. It only works on the local bbs that I hang out on. Sorry. I
    couldn't think of the simple case that could isolate my problem.

    #!/usr/bin/python

    #The original code was written by Mr.Pardue, which sounds a lot like
    #**** you

    import telnetlib, os
    import time
    import glob

    my_porn_collection = ["nude.txt", "nude2.txt", "nude333.txt",
    "nude4.txt", "nude5.txt"]

    path = 'porn/'

    #my_porn_collection = []

    def get_files():
    for infile in glob.glob( os.path.join(path, '*.txt*')):
    my_porn_collection.append(infile)

    def generate(some_naked_bitches):
    try:
    f = open(some_naked_bitches, "r")
    except:
    pass
    art = f.read()
    f.close()
    return art

    def get_name():
    user = raw_input("\nUsername: ")
    password = raw_input("Password: ")
    enter_party(user, password)

    def enter_party(user, password):
    now = telnetlib.Telnet("m-net.arbornet.org")
    now.read_until("login: ")
    print "\nEntered username.."
    now.write(user + "\n")
    now.read_until("Password:")
    now.write(password + "\n" )
    print "Entered pass.."
    now.read_until("m-net%")
    now.write("party\n")
    print "Entered into party.."
    scroll_some_porn(now)

    def scroll_some_porn(now):
    while 1:
    for some_titty_porn in my_porn_collection:
    now.write(" \n")
    bitch_please = generate(some_titty_porn)
    now.write(bitch_please)
    time.sleep(10)
    now.write(" \n")

    if __name__ == "__main__":
    #get_files()
    get_name()
     
    chad, Jun 5, 2009
    #1
    1. Advertising

  2. On Thu, 04 Jun 2009 18:26:49 -0700, chad wrote:

    > Let's say I have a list of 5 files. Now lets say that one of the files
    > reads nude333.txt instead of nude3.txt. When this happens, the program
    > generates an error and quits. What I want it to do is just skip over the
    > bad file and continue on with the next one.



    This should give you some hints.


    import errno

    for name in list_of_file_names:
    try:
    f = open(name, 'r')
    except IOError, e:
    if e.errno == errno.ENOENT: # or just use 2 if you're lazy
    # no such file, skip
    continue
    # some other more serious error, re-raise the exception
    raise
    do_something_with(f)


    [...]
    > def scroll_some_porn(now):
    > while 1:
    > for some_titty_porn in my_porn_collection:
    > now.write(" \n")
    > bitch_please = generate(some_titty_porn)
    > now.write(bitch_please)
    > time.sleep(10)
    > now.write(" \n")


    Shouldn't you have some way of escaping from the infinite loop other than
    typing Ctrl-C at the console? I imagine your hands will be busy.



    --
    Steven
     
    Steven D'Aprano, Jun 5, 2009
    #2
    1. Advertising

  3. chad

    chad Guest

    On Jun 4, 7:36 pm, Steven D'Aprano
    <> wrote:
    > On Thu, 04 Jun 2009 18:26:49 -0700, chad wrote:
    > > Let's say I have a list of 5 files. Now lets say that one of the files
    > > reads nude333.txt instead of nude3.txt. When this happens, the program
    > > generates an error and quits. What I want it to do is just skip over the
    > > bad file and continue on with the next one.

    >
    > This should give you some hints.
    >
    > import errno
    >
    > for name in list_of_file_names:
    >     try:
    >         f = open(name, 'r')
    >     except IOError, e:
    >         if e.errno == errno.ENOENT:  # or just use 2 if you're lazy
    >             # no such file, skip
    >             continue
    >         # some other more serious error, re-raise the exception
    >         raise
    >     do_something_with(f)
    >
    > [...]
    >
    > > def scroll_some_porn(now):
    > >     while 1:
    > >         for some_titty_porn in my_porn_collection:
    > >             now.write(" \n")
    > >             bitch_please = generate(some_titty_porn)
    > >             now.write(bitch_please)
    > >             time.sleep(10)
    > >             now.write(" \n")

    >
    > Shouldn't you have some way of escaping from the infinite loop other than
    > typing Ctrl-C at the console? I imagine your hands will be busy.
    >


    Like what? All the program is meant to do is scroll ASCII art in a
    chat room.
     
    chad, Jun 5, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guy Lateur
    Replies:
    1
    Views:
    301
    Paul McNett
    Jun 9, 2005
  2. Ivan Shevanski
    Replies:
    7
    Views:
    353
    Mike Meyer
    Jun 11, 2005
  3. Krister Svanlund
    Replies:
    4
    Views:
    701
    Ryan Kelly
    Feb 21, 2010
  4. Les Caudle
    Replies:
    1
    Views:
    128
    Dominick Baier [DevelopMentor]
    Jun 2, 2005
  5. aidy
    Replies:
    2
    Views:
    203
    Pit Capitain
    Sep 22, 2006
Loading...

Share This Page