Python - 创建带参数的快捷方式 [英] Python - Create Shortcut with arguments

查看:33
本文介绍了Python - 创建带参数的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 win32com.client,我试图在文件夹中创建一个简单的快捷方式.但是我想要有参数的快捷方式,除非我不断收到以下错误.

回溯(最近一次调用最后一次): 中的文件D:/Projects/Ms/ms.py",第 153 行scut.TargetPath = '"C:/python27/python.exe" "D:/Projects/Ms/msd.py" -b ' + str(loop7)文件C:\Python27\lib\site-packages\win32com\client\dynamic.py",第 570 行,在 __setattr__ 中raise AttributeError("无法设置属性 '%s.%s'." % (self._username_, attr))AttributeError: 无法设置属性.TargetPath".

我的代码看起来像这样.我尝试了多种不同的变体,但似乎无法做到正确.我做错了什么?

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

解决方案

你的代码对我来说没有错误.(Windows XP 32 位,Python 2.7.5,pywin32-216).

(我稍微修改了你的代码,因为 TargetPath 应该只包含可执行路径.)

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

当我尝试跟随(将列表分配给 Arguments 属性.)时,我遇到了与您类似的 AttributeError.

<预><代码>>>>scut.Arguments = []回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件c:\python27\lib\site-packages\win32com\client\dynamic.py",第 570 行,在 __setattr__ 中raise AttributeError("无法设置属性 '%s.%s'." % (self._username_, attr))AttributeError: 无法设置属性.Arguments".

Using win32com.client, I'm attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.

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.

My code looks like this. I've tried multiple different variates but can't seem to get it right. What am I doing wrong?

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

解决方案

Your code works for me without error. (Windows XP 32bit, Python 2.7.5, pywin32-216).

(I slightly modified your code because TargetPath should contain only executable path.)

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

I got AttributeError similar to yours when I tried following (assign list to Arguments property.)

>>> 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.

这篇关于Python - 创建带参数的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆