Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47225
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: How to store a variable when a script is executing for next time execution? |
| Date | 2013-06-06 15:46 +0300 |
| Organization | University of Helsinki |
| Message-ID | <qotwqq7jtkh.fsf@ruuvi.it.helsinki.fi> (permalink) |
| References | <854fde32-9a5a-483e-b135-5d2c0d21b49b@googlegroups.com> |
Avnesh Shakya writes:
> I am running a python script and it will create a file name like
> filename0.0.0 and If I run it again then new file will create one
> more like filename0.0.1...... my code is-
>
> i = 0
> for i in range(1000):
> try:
> with open('filename%d.%d.%d.json'%(0,0,i,)): pass
> continue
> except IOError:
> dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
> break
>
> But It will take more time after creating many files, So i want to
> store value of last var "i" in a variable so that when i run my
> script again then I can use it. for example- my last created file is
> filename0.0.27 then it should store 27 in a variable and when i run
> again then new file should be created 0.0.28 according to last value
> "27", so that i could save time and it can create file fast..
You could get a list of all filenames that match the pattern. Extract
the last components as numbers, and add 1 to the maximum.
i = 1 + max(int(name.split('.')[-1])
for name in glob.glob('filename.0.0.*))
That assumes that there already is at least one such file and all such
files have a last component that can be parsed as an int. Take an
appropriate amount of care.
Or you could also create a file, say lastname.0.0.31, to track the
name, and when you find it there, create filename.0.0.32 and replace
lastname.0.0.32; panic if there is more than one lastname.0.0.*, or
fewer than one.
Or as above but track with nextname.0.0.31 to create filename.0.0.31
and replace the tracking name with nextname.0.0.32 for the next file.
Or save the number somewhere else.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to store a variable when a script is executing for next time execution? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-06-06 03:50 -0700 Re: How to store a variable when a script is executing for next time execution? Cameron Simpson <cs@zip.com.au> - 2013-06-06 21:19 +1000 Re: How to store a variable when a script is executing for next time execution? Avnesh Shakya <avnesh.nitk@gmail.com> - 2013-06-06 17:36 +0530 Re: How to store a variable when a script is executing for next time execution? Dave Angel <davea@davea.name> - 2013-06-06 08:14 -0400 Re: How to store a variable when a script is executing for next time execution? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-06 15:46 +0300 Re: How to store a variable when a script is executing for next time execution? Chris Angelico <rosuav@gmail.com> - 2013-06-07 01:37 +1000 Re: How to store a variable when a script is executing for next time execution? MRAB <python@mrabarnett.plus.com> - 2013-06-06 16:54 +0100 Re: How to store a variable when a script is executing for next time execution? Chris Angelico <rosuav@gmail.com> - 2013-06-07 01:56 +1000
csiph-web