python - 如何从另一个 Python 脚本内部运行一个 Python 脚本?

标签 python linux unix

我目前正在使用子进程在当前的 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/

相关文章:

python - 为什么当我尝试在 1 个函数中执行 2 个条件时,它只给出黑色光栅?

python - 如何创建 pygame 保存文件

unix - 在unix中根据文件名时间戳查找目录中最旧的文件

linux - 如何使用 bash 中的参数从文件中提取行

python - 如果有条件,模块对象在 sleep 状态下不可调用(python)

python - 如何在 Python shell 中导入 NumPy

c/libusb-0.1 : why is usb_release_interface() failing?

linux - 为什么某些已弃用的类在使用 g++ 编译时不会生成警告?

linux - 退格键在 bash 受限模式下不起作用

java - 无法启动测试系统 'slim' : java. io.IOException : Cannot run program "java": error=2, 没有这样的文件或目录