简体   繁体   中英

How to allow python script to replicate the executable file created with pyinstaller?

When I create a.exe file using pyinstaller and run it, I want it to be replicated with the same name in another directory.

I have already written a python script which can replicate itself (.py not.exe) perfectly, but in the case of.exe, I need help from you geniuses.

I think that you should just copy and paste the file. You can do this in python:

https://datatofish.com/copy-file-python/

It uses the shuttle library to copy the file taking in the directory of the file to be copied and the destination:

import shutil
original = r'{}'.format(input('original path where the file is currently stored\\file_name.file_extension:\n'))
print()
target = r'{}'.format(input('target path where the file will be copied\\file name.file extension:\n'))

shutil.copyfile(original, target)

I used the spacing with the empty print statement and the '\n' to make the program more readable when running it, however this is not necessary.

This way we take in user input for the path of the original file and then the target file. This makes it so you can copy and paste any file to any directory with any name inside of python.

When we execute a python script, we can get the script path in argv[0] with the script name. If we convert our script into an --onefile executable with PyInstaller, we can also get the executable path from argv[0]. I tested the following code in windows 10.

import sys
import shutil

target_path = "YOUR TARGET PATH WITH FILE NAME"
shutil.copyfile(sys.argv[0], target_path)

You can also copy the executable with the following method: (not tested)

with open(sys.argv[0], "rb") as file1:
    main_file = file1.read()
    with open(target_path, "wb") as file2:
        file2.write(main_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM