How to increment variable every time script is run in Python?

Viewed 9671

I have a Python script that I want to increment a global variable every time it is run. Is this possible?

4 Answers

example:-

import os
if not os.path.exists('log.txt'):
    with open('log.txt','w') as f:
        f.write('0')
with open('log.txt','r') as f:
    st = int(f.read())
    st+=1 
with open('log.txt','w') as f:
    f.write(str(st))

Each time you run your script,the value inside log.txt will increment by one.You can make use of it if you need to.

Related