Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';{';未被识别为内部或外部命令_Python_Python 2.7 - Fatal编程技术网

Python ';{';未被识别为内部或外部命令

Python ';{';未被识别为内部或外部命令,python,python-2.7,Python,Python 2.7,我是Python新手,正在学习Google开发者教程。我遇到了一个错误-->'运行“python code.py”时,{”不被识别为内部或外部命令。使用下面的代码。我相信我的PATH变量为python设置正确,因为我可以毫无问题地运行其他python代码。有人能给我一些建议吗 import os import sys import commands def List(dir): cmd = 'dir' + dir print 'about to do this:', cmd

我是Python新手,正在学习Google开发者教程。我遇到了一个错误-->'运行“python code.py”时,{”不被识别为内部或外部命令。使用下面的代码。我相信我的PATH变量为python设置正确,因为我可以毫无问题地运行其他python代码。有人能给我一些建议吗

import os
import sys
import commands

def List(dir):
    cmd = 'dir' + dir
    print 'about to do this:', cmd
    (status, output) = commands.getstatusoutput(cmd)
    if status:
        sys.stderr.write('there was an error:'+ output)
        sys.exit(1)
    print output

def main():
    List(sys.argv[1])

if __name__ == "__main__":
    main()
commands模块在Windows上不起作用–它仅适用于Unix。此外,自2.6版以来,它已被弃用,并且在Python 3中已被删除,因此您应该改用subprocess模块。请替换以下行:

import commands
(status, output) = commands.getstatusoutput(cmd)
比如说:

import subprocess
output = subprocess.check_output(['dir', dir])
import subprocess
try:
    output = subprocess.check_output(['dir', dir])
except subprocess.CalledProcessError as e:
    print "Command error: " + e.output
    print "Command output: " + output
    sys.exit(e.returncode)

还将学习Google Python课程。。 My commands.getstatusoutput()替换如下所示:

import subprocess
output = subprocess.check_output(['dir', dir])
import subprocess
try:
    output = subprocess.check_output(['dir', dir])
except subprocess.CalledProcessError as e:
    print "Command error: " + e.output
    print "Command output: " + output
    sys.exit(e.returncode)

运行命令时的参数是什么?确切的输出是什么?
python./script.py。
无法重新生成我像python code.py那样运行命令。确切的输出是“{”,无法识别为内部或外部命令。我只是在Jupyter中运行代码,似乎错误与commands.getstatusoutput有关(cmd)行。`C:\Anaconda2\lib\commands.py in getstatusoutput(cmd)57”““返回(状态,输出)在shell中执行cmd。”“58导入os-->59 pipe=os.popen('{'+cmd+';}2>&1',r')60 text=pipe.read()61 sts=pipe.close()TypeError:无法连接'str'和'builtin_function_或'u method'对象'@chepner,请参见上文什么是
命令
?该错误消息表示传递给
os.popen的命令不完整(可能应该是
os.popen('{'+cmd+'}2>&1',r')
)。