Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Saving/Updating a value

I don’t know how to say this but I need to save a value inside a function, for example a number. Then incrementing by 1 each time it executes. Say it is Operation ID and it is n° 200. It executes five times and it prints 201, 202, 203, 204 and 205. What I need is that 205 is automatically set to next value in case of a future execution of the function.
Let me try to elaborate:

idOperacion = 200  # ID

for i in range(len(trx.index)): # HAS 5 ITEMS
     idOperacion = idOperacion + 1

And then it prints 201, 202, 203, 204 and 205

I want that 205 to be idOperacion the mext time I execute the script with out changing it by hand.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to save you variable within a external file.
This is a lot of ways to save and load data.

Here is a mehtod that fulfill your needs

  • loading
with open('idOperacion') as f:
    idOperacion = int(f.read())
  • saving
with open('idOperacion', 'w') as f:
    f.write(str(idOperacion))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading