0

我制作了一个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!")

4

1 回答 1

0

有诸如当前工作目录(又名 cwd)之类的东西。每当一个进程创建具有相对路径的文件时,这些路径都是相对于 cwd 的。您必须:

  • os.chdir()在调用之前更改目录,os.system()因为 cwd 是从父进程继承的,但 cwd 是进程范围的,os.chdir()从一个线程调用也会影响另一个线程的 cwd,并导致竞争条件

或者

  • 更改传递给的 shell 命令中的目录os.system()

    os.system('cd FOLDER1 && MLTPad1.exe')

于 2020-02-23T02:04:20.840 回答