Commands module in Python, execution error: ' {' is not an internal or external command, or a program or batch file that can be run.

Source: Internet
Author: User

Recently discovered the Python commands module, looked at the next source, using the Popen package, formed three functions GetStatus (), GetOutput (), Getstatusoutput ()

The source code is as follows:

def getstatus (file): "" "    Return output of" Ls-ld <file> "in a string.    " "" Import warnings    Warnings.warn ("commands.getstatus () is deprecated", deprecationwarning, 2)    return GetOutput ( ' Ls-ld ' + mkarg (file) # Get the output from a shell command to a string.# the exit status is ignored; A trailing newline is stripped.# assume the command would work with ' {...;} 2>&1 ' around it. #def getoutput (cmd): "" "    Return output (stdout or stderr) of executing cmd in a shell.    " "" return Getstatusoutput (cmd) [1]# Ditto but preserving the exit status.# Returns a pair (sts, output) #def getstatusoutput (cm d): "" "    Return (status, output) of executing cmd in a shell.    " " Import OS    pipe = Os.popen (' {' + cmd + ';} 2>&1 ', ' r ')    Text = Pipe.read ()    sts = Pipe.close ()    if STS is none:sts = 0    if text[-1:] = = ' \ n ': Text = text[:-1]    return sts, text

By looking at the above source, we found that the main use is function Getstatusoutput ().

When I call this function, the result of each execution is: ' {' is not an internal or external command, nor is it a running program or batch file.

The suspect is getstatusoutput () in this sentence pipe = Os.popen (' {' + cmd + ';} 2>&1 ', ' R ') has a problem. But think of this is the module provided by the official website, it is not supposed to occur this problem.

Put pipe = Os.popen (' {' + cmd + ';} 2>&1 ', ' r '), modify to pipe = Os.popen (cmd, ' R '), and the result is executed correctly.

Do not know whether pipe = Os.popen (' {' + cmd + ';} 2>&1 ', ' r ') This sentence causes the problem? There is the understanding of the great God to know next

Commands module in Python, execution error: ' {' is not an internal or external command, or a program or batch file that can be run.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.