Python 要运行的命令:ls-l';{';不被识别为内部或外部命令、可操作程序或批处理文件

Python 要运行的命令:ls-l';{';不被识别为内部或外部命令、可操作程序或批处理文件,python,ls,Python,Ls,我在hello.py文件中有这样的代码: import sys import os import commands def listdir(dir): cmd = 'ls -l ' + dir print ("Command to run:", cmd) ## good to debug cmd before actually running it (status, output) = commands.getstatusoutput(cmd) if status:

我在hello.py文件中有这样的代码:

import sys
import os
import commands

def listdir(dir):
  cmd = 'ls -l ' + dir
  print ("Command to run:", cmd)   ## good to debug cmd before actually running it
  (status, output) = commands.getstatusoutput(cmd)
  if status:    ## Error case, print the command's output to stderr and exit
    sys.stderr.write(output)
    sys.exit(1)
  print (output)  ## Otherwise do something with the command's output


def main():

 listdir(sys.argv[1])

if __name__ == '__main__':
  main()
我从cmd运行它,如下所示:

$ hello.py .
但有一个错误:

Command to run: ls -l .
'{' is not recognized as an internal or external command,
operable program or batch file.

有人能帮我完善一下我的代码有什么问题吗(我是从谷歌教程中获得的)。python版本是python标准库手册中的2.7版本:

commands.getstatusoutput(cmd) 
Execute the string cmd in a shell with os.popen() and return a 2-tuple (status, output).
cmd is actually run as { cmd ; } 2>&1, so that the returned output will contain output or
error messages. A trailing newline is stripped from the output. The exit status for the
command can be interpreted according to the rules for the C function wait().
语法
{}
由Linux或其他类似Unix的shell正确解释,而不是由Windows
cmd.exe
正确解释


正如其他人建议的那样,尝试使用
子流程
…并阅读手册…

注意,
命令
自2.6版以来就已被弃用,并且在Python 3中已被删除。请改用
子流程
模块。我认为您将此作为一种学习体验,并意识到您是否正在运行tWindows上的his?错误消息的确切措辞表明您是。Windows上的
命令
模块不工作。