0

I have made a quine. it's a mess but I think it will be readable enough. It is made to both write itself to a file and write itself in cmd. When it writes itself in cmd it writes with a short delay between each letter. All of that works.

What doesn't work is that it is supposed to start the new file after having written it. That is to say, it works fine for the first and second file, but at the third it stops.

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)

What seemingly happens is the when stolen2.py runs it doesn't write the "run new file" part. I have no idea why this happens. I'm hoping someone can help me figure this out.

EDIT: something strange I have found out is that the more times you have the below code, the more files are created.

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)
Engesa
  • 9
  • 2
  • I'm not *sure* this is the cause of your issue, but you repeatedly `open` the same file without closing it. Perhaps some of your `write`s are not happening at the right places due to caching? You could either close the file after each time you write to it, or just keep it open until you've done all the writes (get rid of the extra `open` calls). – Blckknght Jun 09 '16 at 07:21
  • This sadly didn't change anything. `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)` – Engesa Jun 13 '16 at 07:53
  • I notice that the lone `file.close()` call you have in the code doesn't get reproduced in the later versions, since it's missing from the `s` string that writes out the surrounding code. If I fix that, the quite runs indefinitely (or at least, for longer than my patience lasts, since the delay you're sleeping for also changes from the first version to the later ones, making them *very* slow). – Blckknght Jun 13 '16 at 09:01
  • OMG YES! Thank you, for spending your time helping a moron. It is much appreciated. – Engesa Jun 13 '16 at 09:55

0 Answers0