简体   繁体   中英

PyInstaller: drag-and-drop files to the --onefile exe

I finally got the PyInstaller up and running. So far it looks good, I'm able to create a single exe that does some calculations on the files in the directory where the exe is located. So I just copy the exe to a folder with the files I need to work on and double click the exe (windows 7).

But would it be possible to make an .exe were I just drag-and-drop my files onto and then it will calculate on those files _ ___ ?

There is a simple way to see how the files you drop on an executable get handled: build an exe from a file with such content:

import sys
def __main__():
    with open("parameters.log", "ab") as f:
        f.write(str(sys.argv))

Use it with one or more files that you drag and drop and observe the content of parameters.log : you should find that for each file its absolute path is passed as an argument. n th file will have its path in sys.argv[n] .

This can actually be generalised to any executable.

Late answer (5Y), but if you drop a file into an exe created with pyinstaller , sys.argv will receive a list containing the path of all the files dragged.
The first item ( [0] ) is the path of the exe , ie:

import sys
print(sys.argv)
['C:/name_of.exe', 'C:/the_file_dragged1', , 'C:/the_file_dragged2']

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