2012-05-08 3 views
1

나는 내가 이것을 실행하면, 내가commands.getstatusoutput sh : 구문 오류 : ";" 예상치 못한

sh: Syntax error: ";" unexpected 

I로 오류가 utilities.py

def execute(command): 
    if not command: 
     return (-1, 'command can not be empty or null') 
    return commands.getstatusoutput(command) 

status = utilities.execute(mongoimport) 

으로 파이썬 명령 모듈을 사용하여 mongoimport 명령을 실행하고 보십시오 documentation는 말합니다 :

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 

이 명령을 실행하려면 어떻게해야합니까?

+1

[명령] (http://docs.python.org/library/commands.html)은 2.6에서 더 이상 사용되지 않으며 3.x에서 제거되었으며 [subprocess] (http://docs.python.org/library)를 사용하십시오. /commands.html) – KurzedMetal

답변

1

사용 subprocess module

from subprocess import check_output 
output = check_output(["ls", "-l"]) 

명령이 실패하지 않는 경우이 오류가 인상됩니다 - 빈 문자열을 검사 할 필요. 당신은 당신이 다음이

output = check_output("ls -l", shell=True) 

처럼 전화 쉘을 통해 물건을 전달하려는 정말 확실하다면 그냥 쉘을 통해 물건을 전달하는 것은 보안 문제에 대한 훌륭한 벡터이다 참고.

관련 문제