Right-click command pass argument to python script

六眼飞鱼酱① 提交于 2020-01-14 03:48:06

问题


I am rewriting a python script at work and part of what it does is searched inside a folder that I right-click on for image sequences.. The python side of things I think I'm good on, but the right click not so much. I can create a right click button in the regedit but I am unsure of how to pass an argument from where I right-clicked on the folder to my python script. I want the argument to have the path to the folder. I found a post about this but it is not working for me.. maybe because they were making it on xp?? I'm unsure.(Link below)

Copy as path in windows context menu

I also found this snip-it of code which is what I think was being used before as the richt-click command..

@="\"C:\Python26\python.exe\" \"L:\HAL\Func\search\search.py\" %L"

Can anyone explain this to me? I understand it first is going to run python then run the script.. but what is the @= and the %L for? I really want to understand this and I spent all day yesterday trying to figure it out but I'm just not finding much. Thanks in advance for your help :)

This is the python script I want to pass the argument too

import sys
import os


def __main__(argv = sys.argv):

    fileName = argv[1]
    print argv
    print fileName

回答1:


The %L should probably be %1. That is, you are taking the first argument passed to this command, and passing it as an argument to your Python script. This will be the name of the file you right-clicked.

If your Python script is prepared to accept multiple arguments (i.e. more than one selected file) you could use %* there.

@ at the beginning of the line means the command line won't be displayed on the screen, so it won't open a console window just to run the command. (If the Python script generates any output, the window will still appear, however.)

Not sure about the = sign following that; I haven't seen it and it's fiendishly hard to Google it.



来源:https://stackoverflow.com/questions/9316907/right-click-command-pass-argument-to-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!