0

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.

PPP
  • 3
  • 3
  • What operating system and desktop environment are you using? Does it allow you to drag a file onto a script to launch the script with the file as an argument? I'm not familiar with that affordance. – John Kugelman Aug 03 '22 at 06:22
  • Win10 operating system, the desktop environment is default, I don't know if I can pass the file as a parameter to the function – PPP Aug 03 '22 at 06:32

1 Answers1

0

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:\venv\my_project\Scripts\python)
  • %* 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).

  • This is a great answer, thanks a lot. But then I thought of a consequent problem. Can I have the same effect if I convert the python script to an exe file? – PPP Aug 03 '22 at 11:52
  • @PPP Good point! I added a section to my answer about creating an executable – Anton Yang-Wälder Aug 03 '22 at 12:07
  • 1
    Thanks for solving my problem perfectly. I also tried it myself and found that there is a method without converting it into exe and bat files: (on Windows operating system) setting the default opening method of python script files to python.exe can also achieve the effect of passing parameters by dragging – PPP Aug 03 '22 at 15:15