Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: UnboundLocalError: local variable 'open' referenced before assignment

Tags:

python

def read_lines():
    readFileName = "readfile.txt"
    f = open(readFileName, 'r+')
    contents = f.read()
        ... # and so on 

read_lines()

When I run this, I get an error:

f = open(readFileName, 'r+')
UnboundLocalError: local variable 'open' referenced before assignment
like image 792
RRR Avatar asked Dec 01 '22 23:12

RRR


1 Answers

This means that further down in your function you create a variable called open:

open = ...

Rename it so that it doesn't clash with the built-in function.

like image 199
NPE Avatar answered Dec 05 '22 07:12

NPE