0

I wrote this code that should give me the percentage of how many bytes I already read from a file a give in input thanks to AppJar. However, when I try to set the value of the bar it goes from 0 to 190% that obviusly is not what I want. Can someone tell me where the error is?

def apriCsv():
 path = app.openBox(title="Apri File", dirName=None, fileTypes=[("File CSV", "*.csv")], asFile=False, parent=None, multiple=False, mode='r')

 size = os.path.getsize(path)
 percentuale = 100.0/size
 letto = 0
 line = ""

 with open(path) as file:
        
        reader = csv.reader(file, delimiter=',')
        
        for riga in reader:

            for i in riga:  #create a string made out of all the element in riga
                    line += i 
           
            letto += len(line.encode('utf-8'))  #get the size in bytes of the string
            app.setMeter("progress", percentuale * letto, percentuale*letto ) #set the value of the bar 
            if riga != ["TITOLO","DESCRIZIONE","PREZZO"]:
                elencoLibri.append(Libro(riga[0], riga[1], riga[2]))
                app.addTableRow("elencoLibri", riga)
   
Gabbo
  • 11
  • 1
  • 5
  • You could either change your `line` builder (`for i in riga: line += i`) to `''.join(riga)` or re-inititalize `line` just before it `line = ""`. You're continuously building on `line` throughout the entire file without clearing after each row. – Axe319 Mar 10 '21 at 12:07
  • I think it worked but it doesn't arrive at 100% but it stops at 85%. Is it a problem with my code or maybe the size of the file is not calculated very well? – Gabbo Mar 10 '21 at 12:33
  • Definitely file size. Try using `line = ','.join(riga)` instead. You're not counting the size of the commas. – Axe319 Mar 10 '21 at 12:56
  • Ok now it is stuck at 95% but at this point I think I'll just add `app.setMeter("progress", 100, "100%" ) ` Thank you very much – Gabbo Mar 10 '21 at 13:41

0 Answers0