简体   繁体   中英

Python program won't read new line in text file

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

try out this simple program that will print each line in your file

f = open("demofile.txt", "r")
for x in f:
  print(x)

Try:

 line = f.read().splitlines()

Use of strip(): .strip() is used with strings, like

  line = 'A rose is a rose is a rose is a rose,\n Gertrude Stein (1874 - 1946)'

   print(line.strip())
   A rose is a rose is a rose is a rose,
      Gertrude Stein (1874 - 1946)

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM