Just like subprocess.Popen( target=, cwd=), it can specify its own local working directory. I dont want to specify the absolute path every time because simple is better than complex. os.chdir() doesn't work at all since it sets a global variable (right?). Whenever there are multiple threads, os.chdir() will fail. Any suggestions? Thank you!
I just try jorgenkg's code and modify a little bit and you may see why I have to ask this question. Here is the code.
import os
import threading
import time
class child(threading.Thread):
def run(self ):
for i in range(1,3):
print "I am " + str(threading.current_thread())[7:15] + " in " + os.getcwd() + '\r\n'
time.sleep(2)
child().start() # prints "/username/path"
os.chdir('C://') # The process is changing directory
child().start() # prints "/"
Here is the Result.
I am Thread-1 in C:\Python27\Projects
I am Thread-2 in C:\
I am Thread-1 in C:\
I am Thread-2 in C:\
You can see that Thread-2 is no long working on its original working directory after os.chdir() is invoked.