在Python中运行Powershell命令(SyntaxError:无效语法)

问题描述 投票:0回答:1

我正在尝试在系统上执行代码,该代码从Windows上的%appdata%目录直接链接下载文件。

我的代码:

def downloadfile():
    mycommand = "powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = {};$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')""
    down = subprocess.call(mycommand)



downloadfile()

但我得到这个错误:

文件“searchmailfolder.py”,第4行mycommand =“powershell -command”$ cli = New-Object System.Net.WebClient; $ cli.Headers ['User-Agent'] ='myUserAgentString'; $ cli.DownloadFile(' https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m','%appdata%\ putty.exe')“”^ SyntaxError:语法无效

python powershell cmd download subprocess
1个回答
0
投票

希望这会有所帮助。导入子进程和sys。然后尝试类似“command = subprocess.Popen([”powershell.exe“,”user_command.ps1“],stdout = sys.stdout)command.communicate()”尝试将代码放入.ps1文件

© www.soinside.com 2019 - 2024. All rights reserved.