-1

It's quite hard to explain personally but basically, I have this Python script that creates thousands of other copies of itself instantly when opened.However i want to add a sort of kill-code that remove all the copies but not the original.# The main file is called "RSV.py" and it's copies are called "RSV-" and then a random HEX code (with the ".py" extension on the end) I apologise for not having the code present, this is due to it being saved on a different system to which i'm writing this on. All help is appreciated, and unless i didn't see it,this is not a duplicate.

Rocky Li
  • 5,641
  • 2
  • 17
  • 33
Omen
  • 19
  • 6
  • As correctly stated by Nagavamsikrishna, this one is indeed very simple, but there is a basic approach for these kind of questions when it's not that simple: use your programming language (Python in this case) for creating a batchfile, which contains the `del ` commands for all concerned files, check it and if correct, launch it. – Dominique Dec 11 '18 at 20:44
  • The problem is that each copy has a different HEX code to each other – Omen Dec 11 '18 at 20:50

4 Answers4

3

rm RSV-*.py should do the trick on Mac or Linux

0

I'd be remiss if I didn't say that Python is a lame way to handle directory maintenance. That said, who am I to tell a man how to delete his files. Try this...

import glob
for file in glob.glob('/tmp/RSV-*.py'):
    os.remove(file)

A-

P.S. Where was the code you tried to write? ...grin.

Adam
  • 3,891
  • 3
  • 19
  • 42
  • Lol, Somewhere else, The code doesn't seem to function. No syntax's or errors but nothing happens – Omen Dec 11 '18 at 21:25
  • Did you... update the path (the snippet that looks like '/tmp/RSV-*.py') with the directory your files are located in? – Adam Dec 11 '18 at 21:50
  • No.Just changed it to : ('c:\Users\omenp\Desktop\RSV-*.py'). and got a .... unicode error! – Omen Dec 11 '18 at 21:59
  • You will need to specify the path with `'c:\\Users\\omenp\\Desktop\\RSV-*.py'` or `'c:/Users/omenp/Desktop/RSV-*.py'` – tshiono Dec 11 '18 at 23:46
  • Flip the slashes. Python always uses forward slashes even on Windows. – Adam Dec 12 '18 at 02:27
0

If you need to stop the python script to create its immediate copies if they're not needed, then there is something wrong in the code itself which is creating them in the first place. However, if you want to just add a line to remove all the excess of python copies of your main script you can add a system call inside the main script to remove all the excess files that are created when the main script finishes its execution.

Here's a simple guide on how to do that. You can replace the used ls -l command with what Nagavamsikrishna mentioned if you are running on Linux/Mac, else just replace rm with del to run on windows.

Calling an external command in Python

kokeen
  • 326
  • 1
  • 2
  • 8
  • No, the code Creates the copies automatically and this was done purposely. As mentioned i want to created a "kill-code" that (incertain scenarios) could delete every copy (basically, a manual fail-safe if all hell breaks loose). – Omen Dec 11 '18 at 21:42
  • That makes sense, may I know which scenario will be this special case? The thing is that with writing a killing script you need to have such a scenario defined. I suggest adding an argument in the script itself or writing a manual script with the link I provided and run it. – kokeen Dec 11 '18 at 21:56
  • That certain scenario would be my computer being destroyed by said duplication script when misused – Omen Dec 11 '18 at 22:03
  • Then I think you need to limit the number of files that are generated from your script. The system might crash if files exceed the capacity of the system. I suggest either adding a catch block to throttle the number of files created, else you won't be able to run the deletion script to remove the excess files. – kokeen Dec 11 '18 at 22:12
0

This will delete all the copies except for the original:

import glob
import os
for file in glob.glob ('DIRECTORY'):
    os.remove(file)
Omen
  • 19
  • 6