简体   繁体   English

在选定文件上运行 Python 脚本

[英]Run Python Script on Selected File

I would like to write a python script that would upload any file I select in Windows Explorer.我想编写一个 python 脚本来上传我在 Windows 资源管理器中选择的任何文件。 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".这个想法是在 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.选择命令后,Python 运行一个脚本,该脚本接收文件路径和要上传的文件的文件名。 The writing the Python script that will upload the file to web seems to be straightforward.编写将文件上传到网络的 Python 脚本似乎很简单。 What is unclear is how to create an entity in Windows Context Menu for the Python Script.不清楚的是如何在 Windows 上下文菜单中为 Python 脚本创建实体。 And how to pass the file path and file name to the Python script to catch.... Please advise!以及如何将文件路径和文件名传给Python脚本来catch....请指教!

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.假设是 Windows 7,如果您打开一个文件夹并在地址栏中键入“shell:sendto”,然后按 Enter,您将被带到上下文菜单。 You can add a .cmd file with the following in it.您可以添加一个 .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.这应该执行您的 python 脚本,将文件 (%1) 作为参数传递。 Within the python script you can use:在 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.这会获取传入的所有参数,因此sys.argv[1]应该会为您获取传入的文件。我对此进行了测试,并且可以正常工作。 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.您需要 .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.请注意,这会在每个单独的文件上调用 python 脚本,如果您想将所有文件作为参数发送到 python 脚本,那么您需要做更多的工作。 You need to research batch scripting if you want to do more advanced things.如果你想做更高级的事情,你需要研究批处理脚本。

@echo off
cls
:upload_loop
IF "%1"=="" GOTO completed
  python C:\Your\File\uploadscript.py %1
  SHIFT
  GOTO upload_loop
:completed

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 limits 2047 for XP and prior, 8191 for windows 7 and later请注意,命令行已为 XP 和之前的版本内置了字符限制2047,对于 Windows 7 和更高版本为 8191

To add things such as python scripts to right click context menu, also possible is to add register keys (regedit) in要将诸如python脚本之类的内容添加到右键单击上下文菜单中,也可以在中添加注册键(regedit)

\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,但我认为这也值得在这里提及。

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.这个网页添加 Windows 上下文菜单操作有一个很好的 python 脚本,它将注册一个上下文菜单以将文件路径传递给你的 python 脚本。 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.另外,这种方式比我猜的 sendTo 解决方案少一键点击。

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:在 regedit 中创建快捷方式:

  1. Press Windows Button+ R, typ in the field "regedit"按 Windows 按钮 + R,在“regedit”字段中键入
  2. Choose where the files need to be placed (I would suggest "HKEY_CURRENT_USER\AllFilesystemObjects\shell & HKEY_CURRENT_USER\AllFilesystemObjects\shellex ". Like that your shortcut shows on files, folder and programm)选择需要放置文件的位置(我建议“HKEY_CURRENT_USER\AllFilesystemObjects\shell & HKEY_CURRENT_USER\AllFilesystemObjects\shellex”。就像你的快捷方式显示在文件、文件夹和程序上一样)
  3. Create a new key in that folder在该文件夹中创建一个新密钥
  4. Create a new key inside your new folder and call i "command"在新文件夹中创建一个新密钥并调用我“命令”
  5. In "command" you now put your file path that needs to be executed.在“命令”中,您现在放置需要执行的文件路径。 On the end of it you put a "%1"在它的最后你放一个“%1”

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)它应该看起来像这样:“C:\Python\folder\file.py”“%1”(是的,它适用于 python 文件,但您也可以使用 cmd 文件)

(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" (我们遇到了一个错误,所以他在我当前的推荐文件上对其进行了一些更改,它看起来像这样:C:\Users\UserName\AppData\Local\Programs\Python\Python38\python.exe "C:\Python\folder\文件.py" "%1"

The reason I cant tell you, but it works :D )我不能告诉你的原因,但它有效:D)

For the python file, I used the following:对于 python 文件,我使用了以下内容:

import sys
sys.argv[1]
print("It worked",sys.argv[1])
while True:
v=1

With that, you have everything you need to have a shortcut and import the path.这样,您就拥有了拥有快捷方式和导入路径所需的一切。

In addition to previous answers, it is also possible to use a python script in the SendTo folder.除了之前的答案,还可以在 SendTo 文件夹中使用 python 脚本。

This allows:这允许:

  • usage of try... except blocks around import statements, in case your library has moved or whatnots;使用try... except import 语句周围的块,以防您的库已移动或其他情况;
  • usage of for loops on the list of arguments.在参数列表中使用for循环。

Example :例子 :

import sys

try:
    import foo
except ImportError as exc:
    print("cannot start!", exc)
    input("press Enter to close")
    sys.exit()

try:
    foo.main(sys.argv[1:])
except Exception as exc:
    print(f"Unexpected error! {exc}")
    input("press Enter to close")
    sys.exit()

input("press Enter to close")

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.我建议将此脚本保持最小,并仅提供构建良好的 Python 包的启动,具有版本控制和经典开发方法。

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

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