我目前正在使用子进程在当前的 Python 中运行 Python 脚本,但它一直给我一个错误:
for dir in os.listdir(os.path.join(DIR2,dirname)):
temp = os.path.join(os.path.join(DIR2,dirname),dir)
files = [os.path.join(temp, f) for f in os.listdir(temp) if f.endswith("json")]
for lists in files:
subprocess.Popen(["python", DIR4, os.path.join(temp,lists)])
以上是我目前正在使用的。
DIR4
是我要运行的 python 的路径。
问题是,我想要运行的 python 一次只能获取一个文件。 然而,这个子进程看起来像是试图一次性执行所有内容。
我想一次运行一个,而不是一次运行所有。 因为它是一次性运行的,所以我想运行的 python 不能按原来的方式工作..
我需要做什么来改变这个?
最佳答案
如果您想先等待子进程终止,然后再继续,我认为您可以使用 Popen.wait():
...
p = subprocess.Popen(["python", DIR4, os.path.join(temp,lists)])
p.wait()
...
关于python - 如何从另一个 Python 脚本内部运行一个 Python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019191/