在选定的文件上运行 Python 脚本
我想编写一个 python 脚本来上传我在 Windows 资源管理器中选择的任何文件。这个想法是在 Windows 资源管理器中选择任何文件,右键单击以显示文件的上下文菜单,然后从那里选择一个命令...例如“上传到 Web 服务器”。
选择命令后,Python 运行一个脚本,该脚本接收要上传的文件的文件路径和文件名。编写将文件上传到网络的 Python 脚本似乎很简单。尚不清楚的是如何在 Windows 上下文菜单中为 Python 脚本创建实体。以及如何将文件路径和文件名传递给Python脚本来捕获....请指教!
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".
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
假设使用 Windows 7,如果您打开一个文件夹并在地址栏中输入“shell:sendto”,然后按 Enter 键,您将进入上下文菜单。您可以添加包含以下内容的 .cmd 文件。
这应该执行您的 python 脚本,并将文件 (%1) 作为参数传递。在 python 脚本中,您可以使用:
这会获取传入的所有参数,因此 sys.argv[1] 应该会获取传入的文件。我对此进行了测试,它可以工作。您需要 .cmd 文件而不是直接访问 .py 的原因是因为 .py 文件不会显示在“发送到”菜单中。
有关获取传入文件的更多信息如下:
在 Python 中接受文件参数(从“发送到”上下文菜单)编辑
:添加用于调用多个文件的脚本。请注意,这会在每个单独的文件上调用 python 脚本,如果您想将所有文件作为参数发送给 python 脚本,那么您需要做更多的工作。如果您想做更高级的事情,您需要研究批处理脚本。
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.
This should execute your python script passing in the file (%1) as a parameter. Within the python script you can use:
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.More information on getting the file passed in is here:
Accepting File Argument in Python (from Send To context menu)
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.
使用
%*
而不是%1
。%1
将传递第一个参数,%*
将传递所有参数(%n
将传递第 n 个参数...)请注意命令行已内置字符限制(对于 XP 及更早版本,字符限制为 2047);对于 Windows 7 及更高版本,字符限制为 8191
Instead of
%1
use%*
.%1
will pass in the first argument,%*
will pass all (%n
will pass in the nth...)Note that the command line has built in character limits 2047 for XP and prior, 8191 for windows 7 and later
要将诸如 python 脚本之类的内容添加到右键单击上下文菜单,还可以添加寄存器键(regedit,免责声明,编辑寄存器时显然要小心!)
,添加一个容器,将其命名为您想要出现在的字符串上下文菜单。在其中添加一个 REG_SZ 类型的键,其中包含 python 脚本启动器,例如
我不知道如何使其与上述解决方案一起使用,以将多个文件选择放入 sys.argv 中,但我认为这值得在这里提及以及。
To add things such as python scripts to right click context menu, also possible is to add register keys (regedit, DISCLAIMER obviously be careful when editing the register !) in
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
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.
此网页 Adding Windows context-menu actions 有一个很好的 python 脚本,它将注册一个上下文菜单以传递python 脚本的文件路径。我没有尝试过,但看起来很容易将此示例修改为您需要执行的操作。另外,我猜这样比 sendTo 解决方案少点击一次。
This webpage Adding Windows context-menu actions has 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.
作为当前的初学者程序员,这些答案都没有帮助。
我问了一个朋友,他帮我解决了。
这是我的解决方案:
在 regedit 中创建快捷方式:
“HKEY_CURRENT_USER\AllFilesystemObjects\shell 和 HKEY_CURRENT_USER\AllFilesystemObjects\shellex”。就像你的快捷方式显示在文件、文件夹和程序上一样)
它应该如下所示:“C:\Python\folder\file.py”“%1”
(是的,它适用于 python 文件,但您也可以使用 cmd 文件)
(我们遇到了一个错误,因此他在我当前的推荐文件上对其进行了一些更改,如下所示:
C:\Users\UserName\AppData\Local\Programs\Python\Python38\python.exe "C:\Python\folder\file.py" "%1"
我不能告诉你的原因,但它有效:D )
对于在 python 文件中,我使用了以下内容:
这样,您就拥有了快捷方式和导入路径所需的一切。
As a current beginner programmer, was none of these answers helpful.
I asked a friend and he helped me with it.
Here is my solution:
Create in regedit a shortcut:
"HKEY_CURRENT_USER\AllFilesystemObjects\shell & HKEY_CURRENT_USER\AllFilesystemObjects\shellex ". Like that your shortcut shows on files, folder and programm)
It should look like this:"C:\Python\folder\file.py" "%1"
(Yes it works with the python file but you can also use a cmd file)
(We had a error so he changed the it a bit on my current commend file it looks like that:
C:\Users\UserName\AppData\Local\Programs\Python\Python38\python.exe "C:\Python\folder\file.py" "%1"
The reason I cant tell you, but it works :D )
For the python file, I used the following:
With that, you have everything you need to have a shortcut and import the path.
除了前面的答案之外,还可以在 SendTo 文件夹中使用 python 脚本。
这允许:
try... except
块,以防您的库已移动或发生其他情况;for
循环。示例:
我建议将此脚本保持最少,并仅提供构建良好的 Python 包的启动,以及版本控制和经典开发方法。
In addition to previous answers, it is also possible to use a python script in the SendTo folder.
This allows:
try... except
blocks around import statements, in case your library has moved or whatnots;for
loops on the list of arguments.Example :
I recommend that this script is kept minimal, and provides just the launch of a well-built Python package, with version control and classical development methods.