I made a python code as below that multiple exe programs run at the same time. However, if I use this code, the output files are created not at each folder but at the folder (Folder0) that the python file is located. Then the output files of the same file name are overlapped in the same folder so that error occurs. How can I make output files being created in each folder, Folder1 and Folder2
python file is located in "c:/Folder0" exe program 1 is located in "c:/Folder0/Folder1" exe program 2 is located in "c:/Folder0/Folder2"
import threading
def exe1():
os.system( '"C:\\Users\\FOLDER0\\FOLDER1\\MLTPad1.exe"' )
def exe2():
os.system('"C:\\Users\\FOLDER0\\FOLDER2\\MLTPad2.exe"')
if __name__ == "__main__":
# creating thread
t1 = threading.Thread(target=exe1, args=())
t2 = threading.Thread(target=exe2, args=())
# starting thread 1
t1.start()
# starting thread 2
t2.start()
# wait until thread 1 is completely executed
t1.join()
# wait until thread 2 is completely executed
t2.join()
# both threads completely executed
print("Done!")
There is such thing as current working directory (a.k.a. the cwd). Whenever a process creates files with relative paths, those paths are relative to the cwd. You have to either:
os.chdir()
before calling os.system()
as cwd is inherited from the parent process, but cwd is process-wide and calling os.chdir()
from one thread will affect the cwd of the other one too and will result in a race conditionor
change the directory within the shell command passed to os.system()
:
os.system('cd FOLDER1 && MLTPad1.exe')