-2

I'm using python for a school project and i want to know how create a simple virus with simple this effect

i tried this: How do I use shutil to make a python file copy itself after doing a calculation?

and this: https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script

then i didn't know how to run the second copy

the code example(I WANT TO RUN THE COPY FOR ONCE)

import os
import shutil

##############

print("hello world")

shutil.copy(__file__, "copy.py") # Copies the file
exec(open("copy.py").read())# Executes the copied file if it is in the same directory as the other file

2 Answers2

0

You can run the second file with execfile(File Path)

So you got something like

import os
import shutil
import fileinput

##############

print("hello world")

shutil.copy(__file__, "copy.py") # Copies the file

file = [Path of the copied File]    

for line in fileinput.FileInput(file, inplace=1):
    if "execfile('copy.py')" in line:
        line=line.rstrip()
        line=line.replace(line,"")
    print (line,end="")
# This could get a problem with large files but should work properly with small files

execfile('copy.py') # Executes the copied file if it is in the same directory as the other file
# Else give it the directory where the file is stored (with / instead of \)
FileX
  • 773
  • 2
  • 7
  • 19
  • can you change my example code please to understand more – Aymen Hmani Mar 04 '21 at 20:49
  • thank you for the code . i just edited my code to become python 3 supported but i want to run the copy for once . when i run the code i get infinite '''hello world''' then this error code: ("RecursionError: maximum recursion depth exceeded while calling a Python object") – Aymen Hmani Mar 04 '21 at 21:09
  • I think my code should now also work for this – FileX Mar 04 '21 at 21:35
  • Edited again so the file gets edited before it gets executed – FileX Mar 05 '21 at 07:32
0

so i get it (i have posted in reddit too)

link: https://www.reddit.com/r/learnpython/comments/lxupy9/how_to_make_a_python_file_copy_itself_then/

import shutil
import subprocess
import os
import random

print("hello world")
new_file_name = str(random.randint(1, 10000)) + ".py"
shutil.copy(__file__, new_file_name)

subprocess.call((new_file_name), shell=True)