windows 在选定文件上运行 Python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8570288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Python Script on Selected File
提问by alphanumeric
I would like to write a python script that would upload any file I select in Windows Explorer. The idea is to select any file in Windows Explorer, right-click to display file's Context Menu and select a command from there... something like "Upload to Web Server".
我想编写一个 python 脚本来上传我在 Windows 资源管理器中选择的任何文件。这个想法是在 Windows 资源管理器中选择任何文件,右键单击以显示文件的上下文菜单,然后从那里选择一个命令......类似于“上传到 Web 服务器”。
After the command is selected, the Python runs a script which receives the file-path and a file name of the file to be uploaded. The writing the Python script that will upload the file to web seems to be straightforward. What is unclear is how to create an entity in Windows Context Menu for the Python Script. And how to pass the file path and file name to the Python script to catch.... Please advise!
选择该命令后,Python 运行一个脚本,该脚本接收要上传的文件的文件路径和文件名。编写将文件上传到网络的 Python 脚本似乎很简单。不清楚的是如何在 Windows 上下文菜单中为 Python 脚本创建实体。以及如何将文件路径和文件名传递给Python脚本来捕捉....请指教!
回答by Tyler Ferraro
Assuming Windows 7, If you open a folder and type "shell:sendto" in the address bar then hit enter you'll be taken to the context menu. You can add a .cmd file with the following in it.
假设使用 Windows 7,如果您打开一个文件夹并在地址栏中键入“shell:sendto”,然后按 Enter,您将被带到上下文菜单。您可以添加一个包含以下内容的 .cmd 文件。
@echo off
cls
python C:\Your\File\uploadscript.py %1
This should execute your python script passing in the file (%1) as a parameter. Within the python script you can use:
这应该执行您的 python 脚本,将文件 (%1) 作为参数传入。在 python 脚本中,您可以使用:
import sys
sys.argv #sys.argv[1] is the file to upload
This gets all parameters passed in so sys.argv[1]
should get you the file that was passed in. I tested this and it works. The reason you need the .cmd file instead of going right to the .py is because the .py file wont show up in the Send To menu.
这将获取传入的所有参数,因此sys.argv[1]
应该为您提供传入的文件。我对此进行了测试,并且可以正常工作。您需要 .cmd 文件而不是直接转到 .py 的原因是因为 .py 文件不会出现在“发送到”菜单中。
More information on getting the file passed in is here:
Accepting File Argument in Python (from Send To context menu)
有关获取传入文件的更多信息,请访问:
在 Python 中接受文件参数(来自发送到上下文菜单)
EDIT: Adding script for calling on multiple files. Note this calls the python script on each individual file, if you want to send all the files as a parameter to the python script then you'll need to do a bit more work. You need to research batch scripting if you want to do more advanced things.
编辑:添加用于调用多个文件的脚本。请注意,这会在每个单独的文件上调用 python 脚本,如果您想将所有文件作为参数发送给 python 脚本,那么您需要做更多的工作。如果你想做更高级的事情,你需要研究批处理脚本。
@echo off
cls
:upload_loop
IF "%1"=="" GOTO completed
python C:\Your\File\uploadscript.py %1
SHIFT
GOTO upload_loop
:completed
回答by Brian
Instead of %1
use %*
.
而不是%1
使用%*
。
%1
will pass in the first argument, %*
will pass all (%n
will pass in the nth...)
%1
将传递第一个参数,%*
将传递所有(%n
将传递第 n 个参数...)
@echo off
cls
python C:\Your\File\uploadscript.py %*
Note that the command line has built in character limits2047 for XP and prior, 8191 for windows 7 and later
请注意,命令行为XP 及更早版本内置了字符限制2047,Windows 7 及更高版本为 8191
回答by user3367775
To add things such as python scripts to right click context menu, also possible is to add register keys (regedit) in
要将诸如 python 脚本之类的内容添加到右键单击上下文菜单中,也可以在
\HKEY_CLASSES_ROOT\Directory\Background\shell
There, add a container, name it the string you want to appear in the context menu. In it, add a key of type REG_SZ, which contains the python script launcher for instance
在那里,添加一个容器,将其命名为要出现在上下文菜单中的字符串。在其中添加一个类型为 REG_SZ 的键,例如包含 python 脚本启动器
C:\Python27\python.exe "C:\path\to\your\script\yourscript.py"
I do not know how to make that work with the aforementionned solution for getting multiple file selections into sys.argv, but I thought this would me worth mentioning here as well.
我不知道如何使用上述解决方案使多个文件选择进入 sys.argv,但我认为这也值得我在这里一提。
回答by otterb
This webpage Adding Windows context-menu actionshas a nice python script that will register a context menu to pass the file path to your python script. I have not tried but it looks easy to modify this sample to what you need to do. Plus, this way it is one click less than sendTo solution I guess.
这个网页添加 Windows 上下文菜单操作有一个很好的 python 脚本,它将注册一个上下文菜单以将文件路径传递给你的 python 脚本。我还没有尝试过,但是将这个示例修改为您需要做的事情看起来很容易。另外,我猜这样比 sendTo 解决方案少单击一次。