在并行 运行 之后在每个文件夹中创建输出文件
making output files being created in each folder after parallel running
我做了一个python代码如下,多个exe程序同时运行。
但是,如果我使用此代码,则输出文件不是在每个文件夹中创建的,而是在 python 文件所在的文件夹 (Folder0) 中创建的。然后相同文件名的输出文件重叠在同一文件夹中,从而发生错误。如何在每个文件夹 Folder1 和 Folder2
中创建输出文件
python 文件位于 "c:/Folder0"
exe 程序 1 位于 "c:/Folder0/Folder1"
exe程序2位于"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!")
有当前工作目录(a.k.a。cwd)。每当进程创建具有相对路径的文件时,这些路径都是相对于 cwd 的。您必须:
- 在调用
os.system()
之前使用 os.chdir()
更改目录,因为 cwd 是从父进程继承的,但 cwd 是进程范围的,从一个线程调用 os.chdir()
将影响 cwd另一个也会导致竞争条件
或
更改传递给 os.system()
的 shell 命令中的目录:
os.system('cd FOLDER1 && MLTPad1.exe')
我做了一个python代码如下,多个exe程序同时运行。 但是,如果我使用此代码,则输出文件不是在每个文件夹中创建的,而是在 python 文件所在的文件夹 (Folder0) 中创建的。然后相同文件名的输出文件重叠在同一文件夹中,从而发生错误。如何在每个文件夹 Folder1 和 Folder2
中创建输出文件python 文件位于 "c:/Folder0" exe 程序 1 位于 "c:/Folder0/Folder1" exe程序2位于"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!")
有当前工作目录(a.k.a。cwd)。每当进程创建具有相对路径的文件时,这些路径都是相对于 cwd 的。您必须:
- 在调用
os.system()
之前使用os.chdir()
更改目录,因为 cwd 是从父进程继承的,但 cwd 是进程范围的,从一个线程调用os.chdir()
将影响 cwd另一个也会导致竞争条件
或
更改传递给
os.system()
的 shell 命令中的目录:os.system('cd FOLDER1 && MLTPad1.exe')