Save variable value in python -


is there way save value of variable (say integer) in python? problem involves calling (entering , exiting) multiple times same python script (python file, not python function) in end creates txt file. i'd name txt files depending on number of times python code called: txt1.txt,..., txt100.txt example.

edit: question not related save parameter in fortran. mistake.

note: i'm assuming mean by:

calling (entering , exiting) multiple times same python code

is want call whole python script multiple times, in case need serialize counter in way external python interpreter, make available next time. if you're asking calling same function or method several times within 1 python session, can variety of ways , i'd point mgilson's answer.

there plenty of ways serialize things, implementation doesn't have language. want store in database? write value file? or enough retrieve appropriate value context? instance, code new file each time called, based on contents of output_dir. it's rough, idea:

import os  def get_next_filename(output_dir):     '''gets next numeric filename in sequence.      files in output directory must have same name format,     e.g. "txt1.txt".     '''      n = 0     f in os.listdir(output_dir):         n = max(n, int(get_num_part(os.path.splitext(f)[0])))     return 'txt%s.txt' % (n + 1)  def get_num_part(s):     '''get numeric part of string of form "abc123".      quick , dirty implementation without using regex.'''      in xrange(len(s)):         if s[i:].isdigit():             return s[i:]     return '' 

or of course can write file called runnum.cfg somewhere next python script, , write current run number it, read when code launches.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -