-1

I have a text file that contains several lines and i want to print each line. From what i know, this code should work:

file = open("text.txt", "r")
lines = file.readlines()
for line in lines:    
    print line
file.close()

But it doesn't, and it gives me a syntax error.. Does anybody know why? I tried using both IDLE and Eclipse(PyDev) and it didn't work in both. My version of python is 3.4 Thanks!

Gambit2007
  • 3,260
  • 13
  • 46
  • 86
  • 1
    If you are using Python 3.4, you should get `SyntaxError: Missing parentheses in call to 'print'`. What is unclear to you about this message? – timgeb Mar 27 '16 at 23:15
  • Well I thought you got it because it's the standard behavior and did not assume that you were using a probably misconfigured IDE :) – timgeb Mar 27 '16 at 23:19
  • I didn't get this message. the message i got was "SyntaxError: invalid syntax" and that's it. – Gambit2007 Mar 27 '16 at 23:19

1 Answers1

1

In Python 3 and above, print is a function not a statement (unlike in Python 2), so you need to do:

file = open("text.txt", "r")
lines = file.readlines()
for line in lines:    
    print(line)
file.close()
TerryA
  • 58,805
  • 11
  • 114
  • 143