Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否将唯一标识符写入脚本?_Python_File_Rewrite - Fatal编程技术网

Python 是否将唯一标识符写入脚本?

Python 是否将唯一标识符写入脚本?,python,file,rewrite,Python,File,Rewrite,我想编写一个下标,在脚本每次运行时为脚本添加一个唯一标识符(机器时间)。但是,每次我编辑脚本(在空闲状态下)时,索引都会被重写。有没有一种优雅的方法可以做到这一点。下面是我写的脚本 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.wri

我想编写一个下标,在脚本每次运行时为脚本添加一个唯一标识符(机器时间)。但是,每次我编辑脚本(在空闲状态下)时,索引都会被重写。有没有一种优雅的方法可以做到这一点。下面是我写的脚本

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()

谢谢您的帮助。

我认为这是一件危险的事情,但它是有效的:

import os, time

print "Hi, ", __file__, '!'

with open(__file__, 'a') as fout:
    fout.write('\n#'+str(time.time())+' s r\n')
请注意,我得到的脚本名称也是
\uuuuu file\uuuu
(但这不是完整的路径名,因此如果更改cwd,可能会出现问题)

还是我在“空闲编辑”这一点上遗漏了什么?当脚本附加到脚本时,您可能无法在编辑窗口中激活该脚本。程序不可能知道哪个有“控制权”