I'm trying to read a text file line by line in order to store the informations into some variables, but it catches an error before the end of the file. While doing line = f.readline().strip(), program refuses to read the next line in file and this error occurs:
Traceback (most recent call last):
File "/Users/JoCarbons/PycharmProjects/untitled/venv/Gestione.py", line 48, in main
line = f.readline().strip()
AttributeError: object has no attribute 'readline'
Here's a piece of the main file:
def main(self):
Veicoli = []
try:
f = open("veicoli.txt", "r")
line = f.readline().strip()
while (line != ''):
tok = line.split()
cod = int(tok[0])
tipo = tok[1]
targa = tok[2]
line = f.readline().strip()
if (tipo == "auto"):
tok = line.split()
cilindrata = int(tok[0])
diesel = bool(tok[1])
line = f.readline().strip()
modello = line
line = f.readline().strip()
marca = line
a = Auto(cod, tipo, targa, cilindrata, diesel, modello, marca)
Veicoli.append(a)
line = f.readline()
else:
#line = f.readline().strip()
categoria = line
line = f.readline().strip()
posti = int(line)
line = f.readline().strip()
modello = line
line = f.readline().strip()
marca = line
f = Furgone(cod, tipo, targa, categoria, posti, modello, marca)
line = f.readline().strip()
Veicoli.append(f)
f.close()
except IOError:
print("IO error found.")
except:
print("Unexpected error: ", sys.exc_info()[0])
raise
The following lines are totally wrong. You are trying to operate file operations on an object which is invalid. Till before "f = Furgone(cod, tipo, targa, categoria, posti, modello, marca)" this line "f" was an object of a file which you opened earlier. After this statement its no longer the file object. So it will not have file related attributes. I am not sure what you are trying to achieve in this statement but if poss you can change the variable name here. Use any other variable for assigning Furgone(cod, tipo, targa, categoria, posti, modello, marca) instead of "f"
f = Furgone(cod, tipo, targa, categoria, posti, modello, marca)
line = f.readline().strip()