Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47222
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <davea@davea.name> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.019 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; 'algorithm': 0.04; 'binary': 0.07; 'variables': 0.07; 'storage,': 0.09; 'subject:script': 0.09; 'try:': 0.09; 'subject:How': 0.10; 'python': 0.11; '(meaning': 0.16; 'dislike': 0.16; 'ioerror:': 0.16; 'loop.': 0.16; 'possible?': 0.16; 'redundant': 0.16; 'subject: \n ': 0.16; 'subject:variable': 0.16; 'subject:when': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'skip:f 30': 0.19; 'separate': 0.22; 'creating': 0.23; 'header:User- Agent:1': 0.23; "aren't": 0.24; 'directory.': 0.24; 'file.': 0.24; 'script': 0.25; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'code': 0.31; 'that.': 0.31; 'existence': 0.31; 'pickle': 0.31; 'subject:next': 0.31; 'file': 0.32; 'run': 0.32; 'running': 0.33; 'entirely': 0.33; 'subject:time': 0.33; 'could': 0.34; 'created': 0.35; 'except': 0.35; 'something': 0.35; 'case,': 0.35; 'one,': 0.35; 'but': 0.35; 'there': 0.35; 'var': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'should': 0.36; 'starting': 0.37; 'checks': 0.38; 'easiest': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'planning': 0.38; 'to:addr:python.org': 0.39; 'according': 0.40; 'how': 0.40; 'received:172.20': 0.60; 'break': 0.61; 'skip:o 30': 0.61; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'save': 0.62; 'name': 0.63; 'become': 0.64; 'more': 0.64; 'reads': 0.68; 'received:74.208': 0.68; 'limit': 0.70; 'housekeeping': 0.84; 'persistent': 0.84; 'received:74.208.4.194': 0.84; 'opens': 0.91; 'gaps': 0.93 |
| Date | Thu, 06 Jun 2013 08:14:33 -0400 |
| From | Dave Angel <davea@davea.name> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: How to store a variable when a script is executing for next time execution? |
| References | <854fde32-9a5a-483e-b135-5d2c0d21b49b@googlegroups.com> |
| In-Reply-To | <854fde32-9a5a-483e-b135-5d2c0d21b49b@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:EyuBnwJ2l9cysgfYYd1H3fRhVxNspnVOp+XOtmFxVi6 NVMYy04Dwv8BsxHeY7tgsPId2SPrTKMVLxoG4zurUTLePho8Xi cGnBSCkUNEALa1BGamg3d0ji9wEvOC5DhgegWsjeYcLCekl+D4 W/uv0EBPPUrCxvSHrMfDJnLivnMZw8UaarjNmYxarberKSOAgo 2Qr1vgZTYUBT6nHk+IW171CLdxvMeexmRK89e4E8S3pe5rlLB3 zLRn/2bG1uE1utYoCyC2Z1zptpyFlvlyYVHRWGZJWRmOorv5fu 1+NY8zymljKEsH9NjwLKbMTuiRSHqOEekrhY/KxmLP8OqANmQ= = |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2802.1370521189.3114.python-list@python.org> (permalink) |
| Lines | 46 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1370521189 news.xs4all.nl 15871 [2001:888:2000:d::a6]:50828 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:47222 |
Show key headers only | View raw
On 06/06/2013 06:50 AM, Avnesh Shakya wrote:
> hi,
> 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
Redundant initialization of i.
> 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
There are no variables once the program ends. You mean you want to
store it in the file. That's known as persistent storage, and in the
general case you could use pickle or something like that. But in your
simple case, the easiest thing would be to simply write the last value
of i out to a file in the same directory.
Then when your program starts, it opens that extra file and reads in the
value of i. And uses that for the starting value in the loop.
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..
>
> Please give me suggestion for it.. How is it possible?
> Thanks
>
Incidentally, instead of opening each one, why not check its existence?
Should be quicker, and definitely clearer.
Entirely separate suggestion, since I dislike having extra housekeeping
files that aren't logically necessary, and that might become out of synch :
If you're planning on having the files densely populated (meaning no
gaps in the numbering), then you could use a binary search to find the
last one. Standard algorithm would converge with 10 existence checks if
you have a limit of 1000 files.
--
DaveA
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