python - TypeError: Can't convert 'int' object to str implicity -


i'm trying read last line of file. , grab first 2 digits in file , add 1 them can write file new product.

with io.open('/home/jake/projects/stock','r+', encoding='utf8' f:     line in f:         if newproduct == line[3:]:              print ("you sell product, if wish add more stock please return menu")         else:              productprice = input("input price product >> ")              productamount = input("how many have in stock >> ")              last_line = f.readlines()              new = last_line[-1]+int(1)              = (new, newproduct)              togetherv2 = (new, productprice)              togetherv3 = (new, productamount) 

the file i'm reading , appending in format

01 tomatos 02 chocolate 

etc... know haven't added part grab first 2 numbers file yet, i'm trying grab whole line , adapt first 2 numbers haven't managed to. know possible duplicate couldn't understand or make solution work. in advance.

the contents of last_line string, "03 apples". you're trying add integer 1 string, raises conversion error.

to slice value portion, , explicitly convert int type (which allow add other integer, this:

new = int(last_line[-1][:1]) + 1 

Comments