1

I have a python program which runs via task scheduler every 1 min in windows 10 enterprise. One run of that program needs to store just an int value and the next run will read it and update for the next run to read and so on.

I don't want to use a DB for this small task and storing the number in a text file looks very raw.

Any suggestions?

Akshay J
  • 5,362
  • 13
  • 68
  • 105
  • What about [python script to read and write a path to registry](https://stackoverflow.com/questions/15128225/python-script-to-read-and-write-a-path-to-registry) – JonSG Jun 09 '23 at 13:25

2 Answers2

1

According to official documentation using files in APPDATA folder is ok. If you are looking for a Python solution - try platformdirs package

Andrei Evtikheev
  • 331
  • 2
  • 12
1

I found a work around to your problem, but it seems way more complex then just writting a log file to keep count of the number of execution. None the less here is the idea, you keep a viariable in your script for the counting of execution. Then when the script finish, you start another task (task scheduler) that modify your first python script to increment the counting variable. Here is an exemple:

You first script file is: count.py

counting = 0

print(f"Hello im counting {counting}! ")

Then your second python script: countIncrementor.py

with open("count.py") as f:
    lines = f.readlines()

newLines = []

newVariable = ""
for line in lines:
    if "counting = " in line:
        splitLine = line.split()

        splitLine[-1] = str(int(splitLine[-1]) + 1)
        for i in splitLine:
            newVariable += i
            newVariable += " "
        newVariable = newVariable[0:-1] + "\n"
        line = newVariable
    
    newLines.append(line)

with open("count.py", 'w') as f:
    for line in newLines:
        f.write(line)

When you run the file countIncrementor.py the file count.py will become:

counting = 1

print(f"Hello im counting {counting}! ")

When you execute your script you then have the information on the number of execution.

Or you could overwrite your script file directly (Assuming a script name test.py):

Before:

counting = 0

with open("test.py") as f:
    lines = f.readlines()

newLines = []
flag = False
newVariable = ""
for line in lines:
    if "counting = " in line:
        if not flag:
            flag = True
            splitLine = line.split()
            print(splitLine)
            splitLine[-1] = str(int(splitLine[-1]) + 1)
            for i in splitLine:
                newVariable += i
                newVariable += " "
            newVariable = newVariable[0:-1] + "\n"
            line = newVariable
    
    newLines.append(line)



with open("test.py", 'w') as f:
    for line in newLines:
        f.write(line)

After:

counting = 1

with open("test.py") as f:
    lines = f.readlines()

newLines = []
flag = False
newVariable = ""
for line in lines:
    if "counting = " in line:
        if not flag:
            flag = True
            splitLine = line.split()
            print(splitLine)
            splitLine[-1] = str(int(splitLine[-1]) + 1)
            for i in splitLine:
                newVariable += i
                newVariable += " "
            newVariable = newVariable[0:-1] + "\n"
            line = newVariable
    
    newLines.append(line)



with open("test.py", 'w') as f:
    for line in newLines:
        f.write(line)
Nathan
  • 83
  • 5