storing variable value

Dave Angel davea at ieee.org
Sat Apr 11 15:30:40 CEST 2009



Daniel Dalton wrote:
> Hi!
>
> I'm writing a program to provide me with battery warnings when my
> battery hits certain levels. It just checks the current level and does
> something. I plan to call it from a a cron job. But If the cron runs
> every minute, warnings every minute would be rather annoying. so is
> there a way to make the script check if it has already ran before?
> eg. can I write to a variable on one run of the program, and on the next
> read that value that was written?
>
> Thx!
>
> Daniel.
>
>   
To put it simply, you want to store a value from one run of the script, 
that persists till the next run of the same script.

The most portable way to to that is to write a file.  And since all you 
care about is the time of writing, the file can be zero length.  So 
simply check for the file, determine it's creation time, and decide if 
it's too recent to want to run again.  If the file doesn't exist, or is 
"old", then write the file and notify the user.


It's still messy to leave this bogus file around, so think carefully 
about where to put it.  If the source directory is writable, it's 
reasonble to me to simply put it there.  That way, if the program gets 
deleted, it'll probably get deleted at the same time.  Failing that, you 
could put it in a TEMP directory.  And perhaps the sneakiest place to 
put it, if you're on Windows, is in the "run once on startup" 
directory.  Make it a do-nothing batch file, and Windows will remove it 
next time the user restarts the system.





More information about the Python-list mailing list