Search code examples
pythoneol

Python simple loop EOL while scanning string literal


I am trying to write out line by line of a text file however I keep getting the following error:

File "E:\Print\test.py", line 8 print "(lines)
SyntaxError: EOL while scanning string literal

myfile = open('log.txt', 'r')
count =  0
while 1:
    lines = myfile.readline()
    if not(lines):
        break
    count = count + 1
    print "(lines)
myfile.close()

any help appreciated


Solution

  • You forgot to close your double quotes after you opened them, or maybe you put one by accident:

    print "(lines)
    

    Here is your edited code:

    myfile = open('log.txt', 'r')
    count =  0
    while 1:
        lines = myfile.readline()
        if not(lines):
            break
        count = count + 1
        print(lines)
    myfile.close()
    

    A SyntaxError: EOL while scanning string literal basically means that it was looking for another quote to end the string, but it came to the End Of the Line (hence EOL), and it didn't find anything.