Is it possible to pass arbitrary arguments to a function in a non-input way?

Question:

Is there a shortcut method such as dragging a file to pass the filename of the dragged file as a parameter to the open() function.
For example, there are two files 00.py and 11.py. I drag 11.py onto 00.py and let go (open 11.py with 00.py), while "11.py" (file name) is passed as an argument to the open() function in 00.py.

Asked By: PPP

||

Answers:

Hi @PPP and thanks for this interesting question!

I found a related answer here on stackoverflow and used it as used it as a starting point.

Enabling drag-and-drop

Looks like you can’t just drag-and-drop onto any kind of file. It has to be an "executable" file (e.g. .exe, .bat, etc.). I set up a minimal example on my machine:

enter image description here

So I can’t drag-and-drop drag_me.txt directly onto opener.py, but drag-and-drop works with drop_here.bat (because Windows recognizes the batch job as "executable" file)

Output

In the console output below, I redacted the absolute path of drag_me.txt for privacy reasons

Reading file 'C:<redacted_for_privacy>drag_me.txt'

Hello!

Press Enter to exit...

drag_me.txt

This is the file you want to open with your Python script. In this example, it just contains the string Hello!

drop_here.bat

This batch job "accepts" the dropped file and calls your Python script.

python opener.py %*
  • python opener.py executes the Python script using the system interpreter. If you’re using a virtual environment, replace python with the path to the interpreter you’re using (e.g. C:venvmy_projectScriptspython)
  • %* passes all arguments into the Python script. In this case, "all arguments" corresponds to drag_me.txt

opener.py

This is the Python script that opens drag_me.txt and prints its contents. The filename is taken from sys.argv, which contains the command line arguments:

The list of command line arguments passed to a Python script. argv[0] is the script name

argv[0] is the script name (opener.py), so argv[1] is our desired filename.

import sys
filename = sys.argv[1]

print(f"Reading file '{filename}'n")

with open(filename) as fp:
    data = fp.read()
    
print(data)
input("nPress Enter to exit...")
  • The input statement isn’t required. It just keeps the command prompt open so you have a chance to see the console output.

Fun fact

You can also execute the batch job from the command line:

drop_here drag_me.txt

Alternative: Create an executable

Instead of using the batch job, you can create an executable from your Python script (e.g. using PyInstaller):

pip install pyinstaller
pyinstaller opener.py --onefile

With the --onefile option, everything is packed into a single .exe file instead of a folder containing multiple file.

Here, I copied opener.exe from the dist folder:

enter image description here

I should note, however, that using PyInstaller comes with its own caveats (e.g. having to make sure everything is packed correctly).

Answered By: Anton Yang-Wälder
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.