我搞乱了自我修改代码的想法,我想使用 py2exe 将文件更改为 .exe。我是否仍然可以让代码自行修改,还是需要将其保留为 .py?
自修改代码:
self = "Strain_0.py"
def Write_self(lis):
ws = open(self, "a")
lis.append("t = open('database.txt', 'a')\n")
lis.append("t.write('Success%s')\n"%(self))
lis.append("t.close()\n")
lis.append("ran = success\n")
lis.append("return ran\n")
lis.append(" except:\n")
lis.append("print('failed')\n")
lis.append("t = open('database.txt', 'a')\n")
lis.append("ran = 'failed'")
lis.append("t.write('failed%s')\n"%(self))
lis.append("t.close()\n")
lis.append("return ran\n")
ws.write("def test():\n")
ws.write(" try:\n")
for i in lis:
if i != "except:\n"and " " not in i:
ws.write(" "+i)
else:
ws.write(i)