How to copy the script by running to another folder in Python?

Asked

Viewed 61 times

4

I made a script in Python that runs some commands in the terminal, and I want it to move to another folder.

I tried to use the cp teste.py minha_pasta/teste.py, but it doesn’t work. This is my code:

def exec_commands(args):

    data = args

    for f in data:

        if f[:2] == 'cd':

            if f[3:] == '~':
                print(os.environ['HOME'])
                os.chdir(os.environ['HOME'])

            else:
                os.chdir(f[3:])

        if len(f) > 0:
            cmd = subprocess.Popen(f[:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE )
            output_bytes = cmd.stdout.read()
            output_str = str(output_bytes, "utf-8")


exec_commands(["mkdir minha_pasta", "cp teste.py minha_pasta/teste.py"])

1 answer

5


If you want to write the script, use the method copy() library shutil.

from shutil import copy

#Nesse exemplo foi criada previamente uma pasta chamada "nova_pasta"

print("#" * 30)
print("Esse script irá se autocopiar!")
print("#" * 30)

copy(__file__,"./nova_pasta/")

Running on Repli.it: https://repl.it/repls/TartIdealisticAdministrator

Browser other questions tagged

You are not signed in. Login or sign up in order to post.