views:

49

answers:

1

I'd like to write a subscript that adds a unique identifier (machine time) to a script everytime that it runs. However, each time I edit the script (in IDLE) the indetifiers are over-written. Is there a elegant way of doing this. The script that I wrote appears below.

import os, time

f = open('sys_time_append.py','r')
lines = f.readlines()
f.close()
fout = open('sys_time_append.py','w')


for thisline in lines:
    fout.write(thisline)
fout.write('\n#'+str(time.time())+' s r\n')
fout.close()

Thanks for any help.

A: 

I expect this is a dangerous thing to do, but it works:

import os, time

print "Hi, ", __file__, '!'

with open(__file__, 'a') as fout:
    fout.write('\n#'+str(time.time())+' s r\n')

Note that I get the name of the script as __file__, as well (but this isn't the full pathname, so there can be problems if the cwd is changed).

Or am I missing something in the reference to "editing in IDLE" that matters here? You probably can't have the script active in an editing window while it's being appended to by the script. It's impossible for the programs to know which has "control".

Andrew Jaffe
That will definitely make funny things happen if you run a .pyc file...
badp