python
  • win32com
  • 2013-07-11 27 views 3 likes 
    3

    使用win32com.client,我试图在文件夹中创建一个简单的快捷方式。然而,我想拥有参数的快捷方式,除了我不断收到以下错误。Python - 使用参数创建快捷方式

    Traceback (most recent call last): 
        File "D:/Projects/Ms/ms.py", line 153, in <module> 
        scut.TargetPath = '"C:/python27/python.exe" "D:/Projects/Ms/msd.py" -b ' + str(loop7) 
    
    File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__ 
        raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr)) 
    AttributeError: Property '<unknown>.TargetPath' can not be set. 
    

    我的代码看起来是这样的。我尝试了多种不同的变量,但似乎无法做到。我究竟做错了什么?

    ws = win32com.client.Dispatch("wscript.shell") 
    scut = ws.CreateShortcut("D:/Projects/Ms/TestDir/testlink.lnk") 
    scut.TargetPath = '"C:/python27/python.exe" "D:/Projects/Ms/msd.py" -b 0' 
    scut.Save() 
    

    回答

    4

    您的代码对我无误。 (Windows XP 32位,Python 2.7.5,pywin32-216)。

    (我稍加修改你的代码,因为TargetPath应该只包含可执行文件的路径。)

    import win32com.client 
    ws = win32com.client.Dispatch("wscript.shell") 
    scut = ws.CreateShortcut('run_idle.lnk') 
    scut.TargetPath = '"c:/python27/python.exe"' 
    scut.Arguments = '-m idlelib.idle' 
    scut.Save() 
    

    我AttributeError的类似你们当我尝试以下(分配列表Arguments属性。)

    >>> scut.Arguments = [] 
    Traceback (most recent call last): 
        File "<stdin>", line 1, in <module> 
        File "c:\python27\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__ 
        raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr)) 
    AttributeError: Property '<unknown>.Arguments' can not be set. 
    
    1

    “..TargetPath应该只包含[an]可执行文件路径。”在两种方式中不正确:

    1. 目标还可能包含可执行文件的参数。

    例如,我有一个文件[d:\ DATA \ CCMD \ Expl.CMD]的代码,其基本线是 启动Explorer.exe “%目标%”

    其使用的一个例子是 D:\ DATA \ CCMD \ Expl.CMD“D:\ DATA \ SYSTEM - 新的安装程序”

    这整行是您所指的“可执行文件”。

    1. 目标不一定是“可执行”。这可能是任何文件,操作系统在里面可以进行操作,比如其默认行为运行可执行与文件作为参数的文件类型,如: “我FILE.TXT”

    的“该文件类型的默认动作是用文本编辑器打开它。实际的可执行文件运行不明确。

    相关问题