File Creation Not Working In A Thread Class?
7stud
bbxx789_05ss at yahoo.com
Sun May 11 15:48:11 EDT 2008
bc90021 wrote:
> 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'
>
> Are threads not allowed to create files?
...oh yeah:
import threading
import time
fname = "data.txt"
f = open(fname)
print f.read()
f.close()
f = open(fname, "a")
f.write("some text\n")
f.close()
f = open(fname)
print f.read()
f.close()
class MyThread(threading.Thread):
def __init__(self, file_name):
threading.Thread.__init__(self)
def run(self):
time.sleep(3)
f = open(fname)
print f.read()
f.close()
f = open(fname, "a")
f.write("other text\n")
f.close()
f = open(fname)
print f.read()
f.close()
my_t = MyThread(fname)
my_t.start()
my_t.join()
--output:--
hello
world
hello
world
some text
hello
world
some text
hello
world
some text
other text
More information about the Python-list
mailing list