Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

like image 258
Char Avatar asked May 16 '17 22:05

Char


People also ask

How do you increment a while loop in Python?

The while loop will check for the condition at the beginning of it. If the condition evaluates to True, it will execute the code inside. Next, we have to use Arithmetic Operator inside the while loop to increment and decrement the value. After the value increments, it will again check the expression.

Is there increment in Python?

There is no Increment and Decrement operators in Python. This may look odd but in Python if we want to increment value of a variable by 1 we write += or x = x + 1 and to decrement the value by 1 we use -= or do x = x - 1 .

What does increments mean in Python?

In computer programming, the action of changing the value of a variable so that it increases is called incrementing a variable. When the variable decreases, we use the verb decrement instead.

How do you increment a variable in Python?

Like most programming languages, Python has a way of including syntactic sugar for scenarios like increment. That said, there is only one true increment operator: +=. To use it, we’ll need to rework our code from before: x = 5

How long does it take to run a python script?

You may now use the following approach to measure the time to run your script: For our example, you may apply the following syntax in Python (make sure that both the pandas and numpy modules are installed first): Once I ran the code on my machine, I got the run time of 5.65 seconds:

What is the increment operator in Python?

Like most programming languages, Python has a way of including syntactic sugar for scenarios like increment. That said, there is only one true increment operator: +=. To use it, we’ll need to rework our code from before:

What happens when x is incremented in Python?

Answer: absolutely nothing. In this example, x is incremented. Then, its previous value is returned, and the result is overwritten. In other words, x stays the same. If that sound wacky, I wrote a whole article about the behavior. It’s one of the reasons I’m glad the syntax never made its way to Python.


2 Answers

Pretty easy to do with an external file, you can create a function to do that for you so you can use multiple files for multiple vars if needed, although in that case you might want to look into some sort of serialization and store everything in the same file. Here's a simple way to do it:

def get_var_value(filename="varstore.dat"):
    with open(filename, "a+") as f:
        f.seek(0)
        val = int(f.read() or 0) + 1
        f.seek(0)
        f.truncate()
        f.write(str(val))
        return val

your_counter = get_var_value()
print("This script has been run {} times.".format(your_counter))

# This script has been run 1 times
# This script has been run 2 times
# etc.

It will store in varstore.dat by default, but you can use get_var_value("different_store.dat") for a different counter file.

like image 111
zwer Avatar answered Nov 12 '22 00:11

zwer


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.

like image 27
abhi Avatar answered Nov 11 '22 22:11

abhi