File Creation Not Working In A Thread Class?

Arnaud Delobelle arnodel at googlemail.com
Sun May 11 14:26:29 EDT 2008


bc90021 <python at bc90021.net> writes:

> On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote:
>
>> bc90021 <python at bc90021.net> writes:
>> 
>>> Hi All,
>>>
>>> Thanks in advance for any and all help!
>>>
>>> I have this code:
>>>
>>> g = open(fileName, 'a')
>>>
>>> where fileName is defined before the line it's used in.  It works fine
>>> when I use it outside a thread class.
>>>
>>> When I put the same line in a thread class, it no longer works, and I
>>> get an error:
>>>
>>> IOError: [Errno 2] no such file u'fileName'
>>>
>>>
>> It's telling you that you haven't got a file called 'fileName'. Posting
>> the code that triggers this error would allow people to diagnose the
>> error accurately rather than guessing.
>
> f = open(otherFile).readlines()
> for i in len(f):
> 	for c in range(0,24,1):
> 		if os.name == "posix":
> 			tempfileName = "\"proctemp/" + self.matrix[c][0] 
> + "_tmp_" + fileName + ".txt\""
>    	if re.search(f[i], pattern):
> 		g = open(tempfileName, 'a')
> 		g.write(f[i])
>
>
> This code works *perfectly* unless I put it in a class that inherits from 
> threading.Thread.  In the thread class, everything works (I can see the 
> "c" value, and I can print out each line in "f[i]", it's just that the g 
> = open line doesn't work.

It's difficult to know what's wrong with the code you posted because:

* it is not self-contained: otherFile, fileName, pattern are names
  which you do not define;

* the IOError you reported earlier can't arise as a result of running
  this code.

* you claim it works unless you put it in a subclass of
  threading.Thread.  Why don't you post this instead, and show us the
  traceback?

HTH

FWIW, my crystal ball (whose predictions I don't usually report!)
tells me the same as Garry Herron's.

-- 
Arnaud



More information about the Python-list mailing list