0

クインを作りました。めちゃくちゃですが、十分に読めると思います。ファイルへの書き込みとcmdへの書き込みの両方を行うように作成されています。cmd で自分自身を書き込むとき、各文字の間に少し遅れて書き込みます。そのすべてが機能します。

うまくいかないのは、新しいファイルを書き込んだ後に開始することになっていることです。つまり、1 番目と 2 番目のファイルでは正常に機能しますが、3 番目のファイルでは停止します。

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)

一見すると、stele2.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)
4

0 に答える 0