问题
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()
回答1:
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.
回答2:
"..TargetPath should contain only [an] executable path." is incorrect in two ways :
- The target may also contain the executable's arguments.
For instance, I have a file [ D:\DATA\CCMD\Expl.CMD ] whose essential line of code is START Explorer.exe "%Target%"
An example of its use is D:\DATA\CCMD\Expl.CMD "D:\DATA\SYSTEM - NEW INSTALL PROGS"
This entire line is the "executable" you are referring to.
- The target doesn't have to be an "executable" at all. It may be any file in which the OS can act upon, such as those file types whose default actions run executable with the files as its arguments, such as : "My File.txt"
The "default action" on this file type is to open it with a text editor. The actual executable file run isn't explicit.
来源:https://stackoverflow.com/questions/17586599/python-create-shortcut-with-arguments