我有以下几行:
#!/root/p34/bin/python
import os
import sys
for i in range(10):
print(i)
currentFile = os.path.abspath(__file__)
print(currentFile)
os.execv(currentFile, sys.argv)
当我尝试从控制台(Ubuntu 14.04)运行时./restart.py我得到:
': [Errno 2] No such file or directory'
当我运行/root/p34/bin/python restart.py
时,我收到python错误:
Traceback (most recent call last):
File "restart.py", line 10, in <module>
os.execv(currentFile, sys.argv)
FileNotFoundError: [Errno 2] No such file or directory
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:2)
os.execv
不寻找shebang线;这是一个shell函数。
使用sys.executable
获取当前Python二进制文件的路径:
os.execv(sys.executable, [sys.executable] + sys.argv)