Home > OS >  A file when you run creates a replica of itself and deletes the original file. (Python)
A file when you run creates a replica of itself and deletes the original file. (Python)

Time:11-17

I tried creating a code where the file, when you run creates a replica of itself and deletes the original file.

Here is my code:

import shutil
import os

loc=os.getcwd()

shutil.move("./aa/test.py", loc, copy_function=shutil.copy2)

But the issue with this is that:

  • this code is only 1 time usable and to use it again, I need to change the name of the file or delete the newly created file and then run it again.

  • Also, If I run it inside a folder, It will always create the new file outside the folder (in a dir up from the exceuting program).

How Do I fix this?

Some Notes:

The copy should be made at the exact place where the original file was.

The folder was empty, just having this file. The file doesn't needs to be in a folder but I just used it as a test instance.

Yes, I understand that if I delete the original file it should stop working. I actually have a figure in my mind of how It should work:

  1. First, a new file with the exact same content in it will be made > in the same path as the original file (with a different name probably).
  2. Then, the original file will be deleted and the 2nd file (which is > the copy of the original file) will be renamed as the exact name and > extension as of the original file which got deleted.

This thing above should repeat every time I run the .py file (containing this code) thus making this code portable and suitable for multiple uses.

Maybe the code to be executed after the file deletion can be stored in memory cache (I guess?).

CodePudding user response:

Easiest way (in pseudo code):

  1. Get name of current script.
  2. Read all contents in memory.
  3. Delete current script.
  4. Write memory contents into new file with the same name.

CodePudding user response:

this code is only 1 time usable and to use it again, I need to change the name of the file or delete the newly created file and then run it again.

That is of course because the file is called differently. You could approach this by having no other files in that folder, or always prefixing the filename in the same way, so that you can find the file although it always is called differently.

Also, If I run it inside a folder, It will always create the new file outside the folder (in a dir up from the exceuting program).

That is because you move it from ./aa to ./. You can take the path of the file and reuse it, apart for the filename, and then it would be in the same folder.

CodePudding user response:

Hey TheKaushikGoswami,

I believe your code does exactly what you told him to and as everybody before me stated, surely only works once. :)

I would like to throw in another idea:

First off I'd personally believe that shutil.move is more of a method for actually moving a file into another directory, as you did by accident. https://docs.python.org/3/library/shutil.html#shutil.move

So why not firstly parameterize your folder (makes it easier for gui/cmd access) and then just copy to a temporary file and then copying from that temporary file. That way you wont get caught in errors raised if you try to create files already existing and have an easy-to-understand code!

Like so:

import shutil
import os

try:
    os.mkdir('./aa/')
except:
    print('Folder already exists!')
 
dest= './aa/'
file= 'test.py'
copypath = dest   'tmp'   file
srcpath = dest   file
shutil.copy2(srcpath, copypath, follow_symlinks=True)
os.remove(srcpath)
shutil.copy2(copypath, srcpath, follow_symlinks=True)
os.remove(copypath)

But may I ask what your use-case is for that since it really doesn't change anything for me other than creating an exact same file?

  • Related