Magical Man Magical Man - 6 days ago 10
Python Question

Syntax error on open() in Python 3, don't know why

SOLVED

I am working out of the "Learn Python the Hard Way" book, and I keep getting a syntax error when trying to open this file, I may just be doing something stupid, and missing something obvious, but I can't figure it out. Any help much appreciated.

from sys import argv

script, input_file = argv

def print_all(f):
print(f.read())

def rewind(f):
print(f.seek(0))

def print_a_line(line_count, f):
print(line_count, f.readline()

current_file = open("input_file")

print("First let's print the whole file: \n")

print_all(current_file)

print("Now let's rewind, kind of like a tape")

rewind(current_file)

print("Finally, let's print three lines:")

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)


The error is being thrown where I try and declare the variable current_file and assign it to open("input_file") Again thank you.

Answer

The line above is missing a ).

print(line_count, f.readline()
Comments