2016-07-31 2 views
4

나는 인자를 가진 다른 프로그램에서 파일을 시작할 파이썬으로 바로 가기를 만들려고한다. 예 :파이썬은 두 개의 경로와 인자로 단축키를 만든다.

"C:\file.exe" "C:\folder\file.ext" argument 

내가 장난 해봤 코드 : 나는 문자열의 다른 형식을 시도했습니다

AttributeError: Property '<unknown>.Targetpath' can not be set. 

:

from win32com.client import Dispatch 
import os 

shell = Dispatch("WScript.Shell") 
shortcut = shell.CreateShortCut(path) 

shortcut.Targetpath = r'"C:\file.exe" "C:\folder\file.ext"' 
shortcut.Arguments = argument 
shortcut.WorkingDirectory = "C:\" #or "C:\folder\file.ext" in this case? 
shortcut.save() 

그러나 나는 오류가 내 길을 던져 Google은이 문제에 대한 해결책을 모르는 것 같습니다.

답변

3
from comtypes.client import CreateObject 
from comtypes.gen import IWshRuntimeLibrary 

shell = CreateObject("WScript.Shell") 
shortcut = shell.CreateShortCut(path).QueryInterface(IWshRuntimeLibrary.IWshShortcut) 

shortcut.TargetPath = "C:\file.exe" 
args = ["C:\folder\file.ext", argument] 
shortcut.Arguments = " ".join(args) 
shortcut.Save() 

Reference

+0

고맙습니다. :) 그러나 나는 두 번째 경로가 문자열 주위에 따옴표를 가지고 있는지 확인하기 위해 빠르며 더러운'path = ' "% s"'% path'을해야했습니다. TargetPath에 넣은 경로는 필요한 경우 자동으로 따옴표를 추가합니다 (경로의 공백) – coco4l

+0

그것이 당신에게 효과가 있다는 소식을 듣고 기쁘게 생각합니다! 솔루션에 만족한다면 대답을 수락 할 수 있습니다. :) – wombatonfire

0

다음은 파이썬 3.6에서 수행하는 방법입니다 (@wombatonfire의 두 번째 가져 오기는 더 이상 발견되지 않습니다).

먼저 난 후, pip install comtypes을했다 :

import comtypes 
from comtypes.client import CreateObject 
from comtypes.persist import IPersistFile 
from comtypes.shelllink import ShellLink 

# Create a link 
s = CreateObject(ShellLink) 
s.SetPath('C:\\myfile.txt') 
# s.SetArguments('arg1 arg2 arg3') 
# s.SetWorkingDirectory('C:\\') 
# s.SetIconLocation('path\\to\\.exe\\or\\.ico\\file', 1) 
# s.SetDescription('bla bla bla') 
# s.Hotkey=1601 
# s.ShowCMD=1 
p = s.QueryInterface(IPersistFile) 
p.Save("C:\\link to myfile.lnk", True) 

# Read information from a link 
s = CreateObject(ShellLink) 
p = s.QueryInterface(IPersistFile) 
p.Load("C:\\link to myfile.lnk", True) 
print(s.GetPath()) 
# print(s.GetArguments()) 
# print(s.GetWorkingDirectory()) 
# print(s.GetIconLocation()) 
# print(s.GetDescription()) 
# print(s.Hotkey) 
# print(s.ShowCmd) 

더 많은 정보를 원하시면 사이트 패키지/comtypes/shelllink.py를 참조하십시오.