调用bash命令的Python函数

2024-06-26 17:37:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个Python函数,它将以bash命令(cmd)作为参数,然后 执行那个命令。在

但是我有一些问题。。。在

这是我的节目:

import subprocess

def main():
    runCommand("ls")
    runCommand("ls -l")
    runCommand("cd /")
    runCommand("ls -l")

def runCommand(cmd):
    subprocess.Popen(cmd)

它适用于像“ls”或“who”这样的命令,但当它变长时,如“ls-l”或“cd/”,它会给我一个错误。在

^{pr2}$

Tags: 函数import命令cmdbash参数maindef