【发布时间】:2025-12-03 04:25:01
【问题描述】:
import os
import sys
pid = os.fork()
print ("second test")
if pid == 0:
print ("this is the child")
print ("I'm going to exec another program now")
os.execl("python", "test.py", * sys.argv)
else:
print ("the child is pid %d" % pid)
os.wait()
我到处查看示例,但对于我的生活,我就是无法理解它。我认为这会起作用,但我收到了这个错误:
Traceback (most recent call last):
File "main.py", line 9, in <module>
os.execl("python", "test.py", * sys.argv)
File "/usr/local/lib/python3.8/os.py", line 534, in execl
execv(file, args)
FileNotFoundError: [Errno 2] No such file or directory
【问题讨论】:
-
execl不进行路径查找。 -
除非这只是一个简化的示例,否则如果您要在子进程中调用
execl,请改用subprocess。
标签: python python-3.x exec