Hi,
I have a script that pulls data off the web based on data in a text file. The text file and script are in the same directory. The values from the script are read into a list. The list is then used in some conditional statements.
The text file has four lines each containing a textual code. So the resulting list has four values.
When the script is run it doesn't read past the 3rd value and gives the following traceback.

Usage: test.py [options]

test.py: error: ticker file Otickers.txt not found
Traceback (most recent call last):
  File "C:\Python25\test.py", line 75, in <module>
    parser.error("ticker file %s not found" % (tickerfile,))
  File "C:\Python25\lib\optparse.py", line 1562, in error
    self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
  File "C:\Python25\lib\optparse.py", line 1552, in exit
    sys.exit(status)
SystemExit: 2

When I run the script with a debugger it gives me a connection reset by peer error and the following traceback. With a debugger it doesn't read past the first value.

Debugger:

Usage: test.py [options]

test.py: error: ticker file Otickers.txt not found
Traceback (most recent call last):
  File "C:\Python25\lib\idlelib\Debugger.py", line 67, in run
    return self.idb.run(*args)
  File "C:\Python25\lib\bdb.py", line 366, in run
    exec cmd in globals, locals
  File "C:\Python25\test.py", line 75, in <module>
    parser.error("ticker file %s not found" % (tickerfile,))
  File "C:\Python25\lib\optparse.py", line 1562, in error
    self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
  File "C:\Python25\lib\optparse.py", line 1552, in exit
    sys.exit(status)
SystemExit: 2

Here is the basic code:

import urllib, datetime, sys
from optparse import OptionParser

if __name__ == '__main__':
    
    parser = OptionParser()
    parser.add_option("-f", "--file", dest="tfile", action="store", default = "t.txt",
                      help="read list from file")     
    (options, args) = parser.parse_args()
    tfile = options.tfile
        
    try:
        file = open(tfile, 'r')
        t = file.readlines()
        for i in range(len(t)):
            if t[i].rstrip('\n') == "TUE":
                f = "tues"
                d = "2"
                print "f:", f
                
            if t[i].rstrip('\n') == "MON":
                f = "mon,"
                d = "1"
                print "f:", f

            file.close()                

            data = urllib.urlopen(url)

            for l in data:
                if l.find(f) != -1:                    
                    r = l.split(',')
                    x = 2
                    while x < 4:                        
                        <<do something>>
                        x = x + 1
                     print "***********************************************************************************************************************"
    except:        
        parser.error("t file %s not found" % (tfile,))
        raise SystemExit

Thank you.

The file Otickers.txt is not in the working directory. Try printing the output of os.listdir('.') to see what actually is in the directory, along with os.getcwd() to see which directory you're actually working in.

also, a major flaw in your code is that any exception caught within your try block will give you the output that opening tfile failed. You should only except the relevant code, which would be the open call. So change your code to look like this:

import urllib, datetime, sys
from optparse import OptionParser

if __name__ == '__main__':
    
    parser = OptionParser()
    parser.add_option("-f", "--file", dest="tfile", action="store", default = "t.txt",
                      help="read list from file")     
    (options, args) = parser.parse_args()
    tfile = options.tfile
        
    try:
        file = open(tfile, 'r')
    except:        
        parser.error("t file %s not found" % (tfile,))
        raise SystemExit

    t = file.readlines()
    for i in range(len(t)):
        if t[i].rstrip('\n') == "TUE":
            f = "tues"
            d = "2"
            print "f:", f
                
        if t[i].rstrip('\n') == "MON":
            f = "mon,"
            d = "1"
            print "f:", f

        file.close()                

        data = urllib.urlopen(url)

        for l in data:
            if l.find(f) != -1:                    
                r = l.split(',')
                x = 2
                while x < 4:                        
                    <<do something>>
                    x = x + 1
                 print "**********************************************************************************************************************"

Also, pretty sure <<do something>> isn't valid syntax :P

Oh, and you should avoid using file as an object name since file is a reserved word in Python.

Thanks jlm,
I will go through the steps you suggest and get back.

Awesome! Making the excepted code more precise and relevant made it work. It was actually an index that was out of range.

Thanks again jlm!

This question has already been answered. Start a new discussion instead.