How do I continue after the error?

C

chad

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()
 
S

Steven D'Aprano

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.
 
C

chad

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top