[Tutor] close failed in file object destructor:

bob gailer bgailer at gmail.com
Tue Oct 18 22:53:47 CEST 2011

Following written before Dave Angel's post but sent afterwards.

On 10/18/2011 1:59 PM, Navneet wrote:
> Hi,
> I am trying to search a list for prime numbers but it's throwing me an 
> error at line no.25.
> I am not able to figure what exactly is the prob
> ne help ??? Error is this:
> $ python "prime1 - Copy.py"
> Unhandled exception in thread started by
> Traceback (most recent call last):
>   File "prime1 - Copy.py", line 25, in findPrime
> close failed in file object destructor:
> Error in sys.excepthook:
> program is below: 

This program could not produce the above exception. Run it. You should get:
     f = open("A;\Numberlist.txt")
IOError: [Errno 2] No such file or directory: 'A;\\Numberlist.txt'

due to the ; in the path name.

If you fix that then you should get:
Traceback (most recent call last):
   File "N:\python\findprime.py", line 25, in findPrime
     for i1 in range(len(c)):               ##this is the 25th line
NameError: global name 'c' is not defined

Please fix these problems and return with a new program and the output 
from that program.

> Numberlist is number range from 1..1000

Huh? What is Numberlist? I only see Numberlist.txt, which I assume is a 
file. How can a file be number range from 1..1000?

Please instead post a sample of the actual file. My *guess* is the file 
looks like:
My guess may be correct, but having to guess wastes all our time.

> import sys
> import threading
> import thread
> import time
> class FindPno():
>     c = []
>     f = open("A;\Numberlist.txt")
>     for i in f:
>         c.append(i)
>     f.close()

replace the above 5 lines with
c = open(corrected path).readlines()
or even better
c = {int(x) for x in ("A;\corrected path)]
then you can dispense with
>     ##print len(c)
>     ##Thread should start here
>     def __init__(self):
>         thread.start_new_thread(self.findPrime,(1,))
>     def findPrime(self,tid):
>         global tlock
> ##        print "I am here"
>         tlock = thread.allocate_lock()
> ##        print "I am here"
>         tlock.acquire()
> ##        print "I am here"
>         for i1 in range(len(c)):               ##this is the 25th line
>             for i2 in range(2,int(c[i1])):
>                 if int(c[i1]) == 1:
>                     print "I am here"
>                     tlock.release()
>                     break
>                 if int(c[i1]) == 2:
>                     print c
Why print the entire list?
>                     print "I am here"
>                     tlock.release()
>                     break
>                 rem = int(c[i1])%i2
>                 if rem == 0:
>                     print "I am here"
>                     tlock.release()
>                     break
>                 if i2 == int(c[i1])-1:
>                     print int(c[i1]), "This is the Thread",tid
>                     print "I am here"
>                     tlock.release()
>         tlock.release()
> if __name__ == '__main__':
>     a = FindPno()

Why did you use a class? You don't need it, and it complicates things.
Why put some of the code in the mainline of the class and some in 
__init__. I see no need for that separation.
Since you use print statements to monitor progress how about addind 
something so you know which print statement was called. Perhaps print "I 
am here 1", print "I am here 2", etc.
Why have the file at all? Why not just start with c = range(1,1000)?

Bob Gailer
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111018/0ff83f7e/attachment-0001.html>

More information about the Tutor mailing list