[TIP] Python 오류

[TIP] Python Development

Python 2.7.x

print "ERROR: ", text, file=sys.stderr

print( )를 이용해 위와 같이 출력시 다음과 SyntaxError가 발생한다. 하지만 Python Document에는 다음과 같이 정의되어 있다.

In [3]:
import sys
In [4]:
def Error1(text):
    print "ERROR :", text, file=sys.stderr
  File "<ipython-input-4-bff5ea818841>", line 2
    print "ERROR :", text, file=sys.stderr
                               ^
SyntaxError: invalid syntax
In [5]:
def Error2(text):
    print("ERROR :", text, file=sys.stderr)
  File "<ipython-input-5-e244cae1c2c5>", line 2
    print("ERROR :", text, file=sys.stderr)
                               ^
SyntaxError: invalid syntax

구글 검색 결과 다음과 같은 부분을 확인할 수 있었다.

따라서 다음과 같이 처리해야 한다.

In [6]:
def Error3(text):
    print >>sys.stderr, "ERROR :", text
In [7]:
Error3("Test")
ERROR : Test

즉, Python Document의 해당 내용은 잘못되었다. Python Document 표현 방식은 Python3.x에서 적용할 수 있는 문법형식이다.

댓글

가장 많이 본 글