Skip to content Skip to sidebar Skip to footer

Python - Increasing Number From The Number That Is Stored In A Text File

accountNum = open('data/accountNum.txt','r') str_size = accountNum.read() size = int(str_size) accountNum.close() At the beginning of the code, I read a text file called accountNu

Solution 1:

Try creating a new file with the same name so that it will overwrite accountNum.txt:

withopen("data/accountNum.txt", 'r') as f:
   str_size = f.read()
   size = int(str_size)
   withopen('data/accountNum.txt', 'w') as f2:
      f2.write(str(size+1))

Note: i did with open because i think it's easier you don't need to add an extra line to close it

Post a Comment for "Python - Increasing Number From The Number That Is Stored In A Text File"