在选定的文件上运行 Python 脚本

发布于 2024-12-22 12:44:36 字数 277 浏览 2 评论 0原文

我想编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

薄情伤 2024-12-29 12:44:36

假设使用 Windows 7,如果您打开一个文件夹并在地址栏中输入“shell:sendto”,然后按 Enter 键,您将进入上下文菜单。您可以添加包含以下内容的 .cmd 文件。

@echo off
cls
python C:\Your\File\uploadscript.py %1

这应该执行您的 python 脚本,并将文件 (%1) 作为参数传递。在 python 脚本中,您可以使用:

import sys
sys.argv  #sys.argv[1] is the file to upload

这会获取传入的所有参数,因此 sys.argv[1] 应该会获取传入的文件。我对此进行了测试,它可以工作。您需要 .cmd 文件而不是直接访问 .py 的原因是因为 .py 文件不会显示在“发送到”菜单中。

有关获取传入文件的更多信息如下:
在 Python 中接受文件参数(从“发送到”上下文菜单)编辑

:添加用于调用多个文件的脚本。请注意,这会在每个单独的文件上调用 python 脚本,如果您想将所有文件作为参数发送给 python 脚本,那么您需要做更多的工作。如果您想做更高级的事情,您需要研究批处理脚本。

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

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.

@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:

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.

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.

@echo off
cls
:upload_loop
IF "%1"=="" GOTO completed
  python C:\Your\File\uploadscript.py %1
  SHIFT
  GOTO upload_loop
:completed
蓝眼泪 2024-12-29 12:44:36

使用 %* 而不是 %1

%1 将传递第一个参数,%* 将传递所有参数(%n 将传递第 n 个参数...)

@echo off
cls
python C:\Your\File\uploadscript.py %*

请注意命令行已内置字符限制(对于 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...)

@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

岛歌少女 2024-12-29 12:44:36

要将诸如 python 脚本之类的内容添加到右键单击上下文菜单,还可以添加寄存器键(regedit,免责声明,编辑寄存器时显然要小心!)

\HKEY_CLASSES_ROOT\Directory\Background\shell

,添加一个容器,将其命名为您想要出现在的字符串上下文菜单。在其中添加一个 REG_SZ 类型的键,其中包含 python 脚本启动器,例如

C:\Python27\python.exe "C:\path\to\your\script\yourscript.py"

我不知道如何使其与上述解决方案一起使用,以将多个文件选择放入 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

\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

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.

面犯桃花 2024-12-29 12:44:36

此网页 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.

篱下浅笙歌 2024-12-29 12:44:36

作为当前的初学者程序员,这些答案都没有帮助。
我问了一个朋友,他帮我解决了。

这是我的解决方案:

在 regedit 中创建快捷方式:

  1. 按 Windows 按钮 + R,在“regedit”字段中键入
  2. 选择文件需要放置的位置(我建议
    “HKEY_CURRENT_USER\AllFilesystemObjects\shell 和 HKEY_CURRENT_USER\AllFilesystemObjects\shellex”。就像你的快捷方式显示在文件、文件夹和程序上一样)
  3. 在该文件夹中创建一个新密钥
  4. 在新文件夹中创建一个新密钥并调用“命令”
  5. 在“命令”中,你现在放置需要执行的文件路径。在其末尾添加一个“%1”,

它应该如下所示:“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 文件中,我使用了以下内容:

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

这样,您就拥有了快捷方式和导入路径所需的一切。

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:

  1. Press Windows Button+ R, typ in the field "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)
  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"

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:

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.

谁许谁一生繁华 2024-12-29 12:44:36

除了前面的答案之外,还可以在 SendTo 文件夹中使用 python 脚本。

这允许:

  • 在导入语句周围使用 try... except 块,以防您的库已移动或发生其他情况;
  • 在参数列表上使用 for 循环。

示例:

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")

我建议将此脚本保持最少,并仅提供构建良好的 Python 包的启动,以及版本控制和经典开发方法。

In addition to previous answers, it is also possible to use a python script in the SendTo folder.

This allows:

  • usage of try... except blocks around import statements, in case your library has moved or whatnots;
  • usage of for loops on the list of arguments.

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.

染火枫林 2024-12-29 12:44:36
#step 1
#create python file name   test.py 
#this is the contenet of test.py
import sys
print(sys.argv)
input('quit ! ')

#step 2 
#convert test.py   to test.exe

#step 3
"""
chose any file and click open with and chose your (test.exe) 
now look the list printed you will understand ! 

    """
#step 1
#create python file name   test.py 
#this is the contenet of test.py
import sys
print(sys.argv)
input('quit ! ')

#step 2 
#convert test.py   to test.exe

#step 3
"""
chose any file and click open with and chose your (test.exe) 
now look the list printed you will understand ! 

    """
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文