top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to store a variable when a script is executing for next time execution in python?

0 votes
587 views

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..

posted Jun 6, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Write it to a file? Read the file next time the script runs?

BTW, trying to open zillions of files is slow. But using listdir to read the directory you can see all the names. Pick the next free one (and then test anyway).

answer Jun 6, 2013 by anonymous
+1 vote

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.

answer Jun 6, 2013 by anonymous
Similar Questions
+2 votes

I am trying to test measure some IO execution in milliseconds , but bit confuse about best method achive that under windows 7. I am using following code but not sure if its best or correct way since i have two different results, which one should i take as results and which is best way. Please advice

code exec results 
100000000 loops, best of 3: 0.00925 usec per loop
3.827947176762156

run code
 import timeit ,time
 import datetime
 import os

 def main():    
 os.path.getsize("c:/video-2011-09-09-09-32-29.mp4")  
 if __name__ == '__main__':    
 t = timeit.Timer('main()')    
 print (t.timeit(1))
+1 vote

I want to build a class that perform various functions to the content(or lines) of any given file. I want to first include the opening and reading file function into that class, then add other methods.

Here is what I wrote for opening file and reading the lines:

class FileOpration:
 def __init__(self,name):
 self.name = name
 self.filename = self.name
 def readAllline(self):
 open(self.name).readlines()

file1 = FileOpration("myfile.txt")
print file1.filename
allines = file1.readAllline()
print allines

I define self.name variable because I want to store the specific file that I read. This works fine by the test code of print file1.filename. I saw "myfile.txt" on the terminal output.

However, the readAllline() method dose not work. The code only give me "None" as the output on terminal

0 votes
x = range (5)
y = range (5)
for ply in x:
 for com in y:
 if ply==com:
 result="Tie"
 print(ply,com,result)

Why is ply always equal to com?

0 0 Tie
0 1 Tie
0 2 Tie
0 3 Tie
0 4 Tie
1 0 Tie
1 1 Tie
1 2 Tie
1 3 Tie
1 4 Tie
2 0 Tie
2 1 Tie
2 2 Tie
2 3 Tie
2 4 Tie
3 0 Tie
3 1 Tie
3 2 Tie
3 3 Tie
3 4 Tie
4 0 Tie
4 1 Tie
4 2 Tie
4 3 Tie
4 4 Tie
...