0

I borrowed some code from this site to increment a number when a python script is run. This works great. What I am trying to do is if the script is run numerous times in one day the variable should not change. When the script is run on a different day the number increments one time for that day. This is the code I borrowed.

def new_num(filename="varstore.dat"):
    with open(filename, "a+") as f:
        val = int(f.read() or 0) + 1
        f.seek(0)
        f.truncate()
        f.write(str(val))
        return val
martineau
  • 119,623
  • 25
  • 170
  • 301
Mike Hirschmann
  • 139
  • 1
  • 1
  • 7
  • I suggest to simply use an "if-statement" and the datetime module... You will have to save the current date, too. – frankenapps Jan 31 '19 at 17:43

2 Answers2

1

You should write the current date into the file along with the current value of the variable. This way next time your script is run it can derive not only the variable value but also the date of the day it was last increased. If this matches the current date, do not increase the variable value. If it doesn't match, increase the value and write it, together with the current date, back into the file.

import datetime

try:
  with open('varstore.dat') as f:
    date_string, val_string = f.read().split()
except FileNotFoundError:
  date, val = None, 0
else:
  date = datetime.datetime.strptime(date_string, '%Y-%m-%d').date()
  val = int(val_string)
today = datetime.date.today()
if date != today:
  val += 1
  with open('varstore.dat', 'w') as f:
    f.write("%s %s" % (today, val))
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • Works great. I added at the end return val so I can use the number in a word docx that I open. Now just to save the file to a specific place so I can start the number at 21. – Mike Hirschmann Jan 31 '19 at 18:21
  • @Mike: Consider using the directory the script is in to store the file. You can get that by calling `os.path.dirname(os.path.abspath(__file__))`. – martineau Jan 31 '19 at 19:45
1

If you do not want to save the current date in the file, you may use os.stat('filename'), here the docs.

os.stat() will give you something similar to this: os.stat_result(st_mode=33188, st_ino=1585598, st_dev=2058, st_nlink=1, st_uid=1000, st_gid=1000, st_size=364, st_atime=1548854761, st_mtime=1548854758, st_ctime=1548854758)

You cat get the st_mtime vaule with os.stat('filename').st_mtime. This is the time when the file was modified last time.

This answer explains how to convert it to a datetime. You can then compare the datetime of last modification with current datetime, and check if a reasonable amount of time is passed or not to increment the counter.

Valentino
  • 7,291
  • 6
  • 18
  • 34
  • Seems like a lot of trouble to go just to avoid also storing the date in the file... – martineau Jan 31 '19 at 18:27
  • @martineau yes, that's true. I just wanted to point out that what the OP wants can be done without saving the date in the file, too. – Valentino Jan 31 '19 at 18:34
  • 1
    That's in fact a nice idea! In general, the file time is not meant to carry that kind of information (time of last increment of the variable) but only the time of the last file change. So keep in mind that after copying the whole project to somewhere (including this file) the file time might be changed to the time of the copying (depends on the copy method) and thus the information of the last variable increment might get lost. So I would not recommend to do it this way as it can lead to hard to find bugs. But this idea was surely worth mentioning! – Alfe Feb 01 '19 at 10:10
  • @Alfe good thing pointing out that copying the file is likely to change the `st_mtime` value. I did not consider that. – Valentino Feb 01 '19 at 12:15