File Creation Not Working In A Thread Class?

bc90021 python at bc90021.net
Sun May 11 14:42:21 EDT 2008


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

Here's the thread class:

#single file is the file we're working on, whose name is passed into the class and which does exist
#matrix is a list of lists that contains info about the files - for this example, [c][0] contains a string, [c][2] contains true or false, and [c][3] contains a pattern to match
#tfValue is another true or false value


class FileProcThread(threading.Thread):
	def __init__(self, singleFile, matrix, tfValue):
        	self.singleFile = singleFile
        	self.matrix = matrix
        	self.tfValue = tfValue
        	threading.Thread.__init__(self)
   	def run(self):
        	(dirName, fileName) = os.path.split(self.singleFile)
        	f = open(self.singleFile).readlines()
        	copying = False
        	for i in range(len(f)):
            		for c in range (len(self.matrix)):
                		if (re.search(self.matrix[c][3], f[i])):
                    			if (self.matrix[c][2] == True):
                        			copying = True
                        			if os.name == "posix":
                            				if (self.tfValue == False):
								tempfileName = "\"proctemp/" + self.matrix[c][0] + "_tmp_" + fileName + 
".txt\""
							else:
                                				tempfileName = "\"proctemp/" + self.matrix[c][0] + "_other.txt\""
                        			else:
                            				if (self.tfValue == False):
								tempfileName = "\"proctemp\\" + self.matrix[c][0] + "_tmp_" + fileName + ".txt\""
                            				else:
								tempfileName = "\"proctemp\\" + self.matrix[c][0] + "_other.txt\""
                    			else:
						copying = False
                		if (re.search(self.matrix[c][4], f[i])):
					copying = False
			if (copying):
                		print "We're in copying, and tempfileName is: %s...\n" % tempfileName
				#The above line correctly prints the temporary file name every time!  The directory exists, too!
		                g = open(tempfileName, 'a')  #This does not work.  Notice I do NOT have quotes around tempfileName, as I said.
		                g.write(f[i])
                		g.close()

Like I said, this works FINE outside the thread class.  I hope that the formatting comes through...



More information about the Python-list mailing list