简体   繁体   English

拖放到 Windows 资源管理器中的 Python 脚本上

[英]Drag and drop onto Python script in Windows Explorer

I would like to drag and drop my data file onto a Python script and have it process the file and generate output.我想将我的数据文件拖放到 Python 脚本上,让它处理文件并生成输出。 The Python script accepts the name of the data file as a command-line parameter, but Windows Explorer doesn't allow the script to be a drop target. Python 脚本接受数据文件的名称作为命令行参数,但 Windows 资源管理器不允许该脚本作为放置目标。

Is there some kind of configuration that needs to be done somewhere for this work?这项工作是否需要在某处进行某种配置?

Sure.当然。 From a mindless technology article called "Make Python Scripts Droppable in Windows" , you can add a drop handler by adding a registry key:名为“Make Python Scripts Droppable in Windows”无意识技术文章中,您可以通过添加注册表项来添加放置处理程序:

Here's a registry import file that you can use to do this.这是您可以用来执行此操作的注册表导入文件。 Copy the following into a .reg file and run it (Make sure that your .py extensions are mapped to Python.File).将以下内容复制到 .reg 文件中并运行它(确保您的 .py 扩展名映射到 Python.File)。

 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\\Python.File\\shellex\\DropHandler] @="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

This makes Python scripts use the WSH drop handler, which is compatible with long filenames.这使得 Python 脚本使用与长文件名兼容的 WSH 丢弃处理程序。 To use the short filename handler, replace the GUID with 86C86720-42A0-1069-A2E8-08002B30309D .要使用短文件名处理程序,请将 GUID 替换为86C86720-42A0-1069-A2E8-08002B30309D

A comment in that post indicates that one can enable dropping on "no console Python files ( .pyw )" or "compiled Python files ( .pyc )" by using the Python.NoConFile and Python.CompiledFile classes.该帖子中的一条评论表明,可以通过使用Python.NoConFilePython.CompiledFile类启用“无控制台 Python 文件 ( .pyw )”或“已编译的 Python 文件 ( .pyc )”。

write a simple shell script ( file.bat )编写一个简单的 shell 脚本( file.bat

"C:\python27\python.exe" yourprogram.py %*

where %* stands for the all arguments you pass to the script.其中%*代表您传递给脚本的所有参数。

Now drag & drop your target files on the file.bat icon.现在将目标文件拖放到file.bat图标上。

With an installed python - at least 2.6.1 - you can just drag and drop any file on a python script.使用已安装的 python - 至少 2.6.1 - 您可以将任何文件拖放到 python 脚本上。

import sys
droppedFile = sys.argv[1]
print droppedFile

sys.argv[0] is the script itself. sys.argv[0]是脚本本身。 sys.argv[n+1] are the files you have dropped. sys.argv[n+1]是您删除的文件。

Try using py2exe.尝试使用 py2exe。 Use py2exe to convert your python script into a windows executable.使用 py2exe 将您的 python 脚本转换为 Windows 可执行文件。 You should then be able to drag and drop input files to your script in Windows Explorer.然后,您应该能够将输入文件拖放到 Windows 资源管理器中的脚本中。 You should also be able to create a shortcut on your desktop and drop input files onto it.您还应该能够在桌面上创建快捷方式并将输入文件拖放到桌面上。 And if your python script can take a file list you should be able to drag and drop multiple files on your script (or shortcut).如果您的 python 脚本可以获取文件列表,您应该能够在脚本(或快捷方式)上拖放多个文件。

Create a shortcut of the file.创建文件的快捷方式。 In case you don't have python open .py files by default, go into the properties of the shortcut and edit the target of the shortcut to include the python version you're using.如果默认情况下您没有 python open .py 文件,请进入快捷方式的属性并编辑快捷方式的目标以包含您正在使用的 python 版本。 For example:例如:

Target: C:\\Python26\\python.exe < shortcut target path>目标:C:\\Python26\\python.exe <快捷目标路径>

I'm posting this because I didn't want to edit the Registry and the .bat workaround didn't work for me.我发布这个是因为我不想编辑注册表,而且 .bat 解决方法对我不起作用。

1). 1)。 create shortcut of .py创建 .py 的快捷方式
2). 2)。 right click -> properties右键单击-> 属性
3). 3)。 prefix "Target:" with "python" so it runs the .py as an argument into the python command前缀“Target:”与“python”,因此它将.py作为参数运行到python命令中
or或者
1). 1)。 create a .bat创建一个.bat
2). 2)。 python some.py %* python some.py %*

these shortcut versions are simplest for me to do what i'm doing这些快捷方式版本对我来说是最简单的
otherwise i'd convert it to a .exe, but would rather just use java or c/c++否则我会将其转换为 .exe,但宁愿只使用 java 或 c/c++

Late answer but none of the answers on this page worked for me.迟到的答案,但此页面上的所有答案都不适合我。
I managed to enable/fix the drag and drop onto .py scripts using:我设法使用以下方法启用/修复拖放到.py脚本上:

  1. HKEY_CLASSES_ROOT\\.py -> Set default value to Python.File HKEY_CLASSES_ROOT\\.py -> 将默认值设置为Python.File

  2. HKEY_CLASSES_ROOT\\Python.File\\Shell\\Open -> Create a key called Command with default value "C:\\Windows\\py.exe" "%1" %* HKEY_CLASSES_ROOT\\Python.File\\Shell\\Open -> 创建一个名为Command的键,默认值为"C:\\Windows\\py.exe" "%1" %*

  3. CLASSES_ROOT\\Applications\\py.exe\\open\\command -> Create keys if the don't exist and set the default value to "C:\\Windows\\py.exe" "%1" %* CLASSES_ROOT\\Applications\\py.exe\\open\\command -> 如果不存在则创建密钥并将默认值设置为"C:\\Windows\\py.exe" "%1" %*

  4. CLASSES_ROOT\\Python.File\\ShellEx -> create key DropHandler with default value {86C86720-42A0-1069-A2E8-08002B30309D} CLASSES_ROOT\\Python.File\\ShellEx -> 使用默认值创建密钥DropHandler {86C86720-42A0-1069-A2E8-08002B30309D}

That's it.就是这样。 Test it by dragging a file onto the python script:通过将文件拖到 python 脚本上来测试它:

import sys

args = sys.argv
print(args)

For those who use argv in .py script but still can't drag files to execute, this could be solved by simply using Python Launcher (with rocket icon)对于那些在.py脚本中使用argv但仍然无法拖动文件执行的人,可以通过简单地使用Python Launcher(带有火箭图标)来解决这个问题

the script property "Open File" was set as python.exe, which has no knowledge that the script needs command-line arguments "%*"脚本属性“打开文件”被设置为 python.exe,它不知道脚本需要命令行参数“%*”

Refer to: https://bugs.python.org/issue40253参考: https : //bugs.python.org/issue40253

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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