Python quine在3次迭代后停止运行。没错

时间:2016-06-09 06:51:45

标签: python quine

我做了一个quine。这是一团糟,但我认为它足够可读。 它既可以将自身写入文件,也可以在cmd中自行编写。 当它在cmd中写入时,它会在每个字母之间写入一个短暂的延迟。 所有这些都有效。

什么不起作用是它应该在写完后启动新文件。也就是说,它适用于第一个和第二个文件,但第三个文件停止。

import time
import sys
import re
import os

#Figure out the number of the current file, and add 1.
name = os.path.basename(__file__)
t = str(int(re.findall(r'\d+',name)[0])+1)

#The first string to write.
s='\n\n\nimport time ' \
  '\nimport re' \
  '\nimport os' \
  '\nimport sys ' \
  '\n\nname = os.path.basename(__file__)' \
  '\nt = str(int(re.findall(r"\d+",name)[0])+1)' \
  '\n\ns=%r ' \
  '\n\nfor l in s%%s:' \
  '\n   sys.stdout.write(l)' \
  '\n   sys.stdout.flush()' \
  '\n   time.sleep(0.05) ' \
  '\n\nfile = open("stolen"+t+".py", "w+") ' \
  '\nfile.write(s%%s)'

#Write the above string slowly in cmd
for l in s%s:
   sys.stdout.write(l)
   sys.stdout.flush()
   time.sleep(0.00005)

#Create and open the new file and write to it
file = open("stolen"+t+".py", "w+")
file.write(s%s)

#second string to write
s='\n\ns=%r ' \
  '\n\nfor l in s%%s:' \
  '\n   sys.stdout.write(l)' \
  '\n   sys.stdout.flush()' \
  '\n   time.sleep(0.05) ' \
  '\n\nfile = open("stolen"+t+".py", "a") ' \
  '\nfile.write(s%%s)'

#write slowly
for l in s%s:
   sys.stdout.write(l)
   sys.stdout.flush()
   time.sleep(0.00005)

#write to file
file = open("stolen"+t+".py", "a")
file.write(s%s)


#new string
s='\n\ns=%r ' \
  '\n\nfor l in s%%s:' \
  '\n   sys.stdout.write(l)' \
  '\n   sys.stdout.flush()' \
  '\n   time.sleep(0.05) ' \
  '\n\nfile = open("stolen"+t+".py", "a") ' \
  '\nfile.write(s%%s)'

#write slowly
for l in s%s:
   sys.stdout.write(l)
   sys.stdout.flush()
   time.sleep(0.00005)

#write to file
file = open("stolen"+t+".py", "a")
file.write(s%s)

#new string
s='\n\ns=%r ' \
  '\n\nfor l in s%%s:' \
  '\n   sys.stdout.write(l)' \
  '\n   sys.stdout.flush()' \
  '\n   time.sleep(0.05) ' \
  '\n\nfile = open("stolen"+t+".py", "a") ' \
  '\nfile.write(s%%s)' \
  '\n\nimport subprocess' \
  '\n\nsubprocess.call("python stolen"+t+".py 1", shell=True)'

#write slowly
for l in s%s:
   sys.stdout.write(l)
   sys.stdout.flush()
   time.sleep(0.00005)

#write to file
file = open("stolen"+t+".py", "a")
file.write(s%s)
file.close()

#forgot to import this
import subprocess

#run new file
subprocess.call("python stolen"+t+".py 1", shell=True)

看似发生的事情是当stolen2.py运行时它没有写“运行新文件”部分。我不知道为什么会这样。 我希望有人可以帮我解决这个问题。

编辑: 我发现的一些奇怪的事情是你拥有以下代码的次数越多,创建的文件就越多。

s='\n\ns=%r ' \
  '\n\nfor l in s%%s:' \
  '\n   sys.stdout.write(l)' \
  '\n   sys.stdout.flush()' \
  '\n   time.sleep(0.05) ' \
  '\n\nfile = open("stolen"+t+".py", "a") ' \
  '\nfile.write(s%%s)'

#write slowly
for l in s%s:
   sys.stdout.write(l)
   sys.stdout.flush()
   time.sleep(0.00005)

#write to file
file = open("stolen"+t+".py", "a")
file.write(s%s)

0 个答案:

没有答案