如何从另一个 Python 脚本中 运行 一个 Python 脚本?
How to run a Python script from inside of another Python Script?
我目前正在使用子进程来 运行 我当前 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 一次只能获取一个文件。
然而,这个子进程看起来像是试图一次执行所有。
我想 运行 一次一个,而不是一次全部。
因为它是一次 运行ning ALL,所以我想要 运行 的 python 不能像现在这样工作..
我需要做什么来改变这个?
如果您想先等待子进程终止,然后再继续,我想您可以使用 Popen.wait():
...
p = subprocess.Popen(["python", DIR4, os.path.join(temp,lists)])
p.wait()
...
要真正按照您的要求进行操作,而不是通过子流程将其组合在一起,您可以使用 exec
,它允许您使用自己提供的全局变量 运行 python 代码和当地人。
在 Python 的旧版本(意思是 pre-3)中,您可以使用 execfile
to achieve the same thing。
我目前正在使用子进程来 运行 我当前 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 一次只能获取一个文件。 然而,这个子进程看起来像是试图一次执行所有。
我想 运行 一次一个,而不是一次全部。 因为它是一次 运行ning ALL,所以我想要 运行 的 python 不能像现在这样工作..
我需要做什么来改变这个?
如果您想先等待子进程终止,然后再继续,我想您可以使用 Popen.wait():
...
p = subprocess.Popen(["python", DIR4, os.path.join(temp,lists)])
p.wait()
...
要真正按照您的要求进行操作,而不是通过子流程将其组合在一起,您可以使用 exec
,它允许您使用自己提供的全局变量 运行 python 代码和当地人。
在 Python 的旧版本(意思是 pre-3)中,您可以使用 execfile
to achieve the same thing。