且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:返回ksh函数的输出

更新时间:2023-12-04 10:39:10

shlex并未执行您想要的操作:

shlex is not doing what you want:

>>> list(shlex.shlex("/bin/ksh -c \". /Home/user/.khsrc\""))
['/', 'bin', '/', 'ksh', '-', 'c', '". /Home/user/.khsrc"']

您正在尝试执行根目录,但不允许这样做,因为它是目录而不是可执行文件.

You're trying to execute the root directory, and that is not allowed, since, well, it's a directory and not an executable.

相反,只需给subprocess.call列出程序名称和所有参数的列表即可:

Instead, just give subprocess.call a list of the program's name and all arguments:

import subprocess

command_line = ["/bin/ksh", "-c", "/Home/user/.khsrc"]
subprocess.call(command_line)