python - 在 Python 中运行 Powershell 命令(SyntaxError:无效语法)
问题描述
我正在尝试在系统上执行代码,该代码将文件从直接链接下载到%appdata%
Windows 上的目录。
我的代码:
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: 无效语法
解决方案
希望这有帮助。导入子进程和系统。然后尝试类似“command = subprocess.Popen(["powershell.exe","user_command.ps1"],stdout=sys.stdout) command.communicate()" 尝试将代码放入 .ps1 文件
推荐阅读
- java - StringBuilder 有时会导致线条错乱
- swift4 - Instamojo 集成问题
- azure-service-fabric - 服务结构资源管理器文件部署问题(apim.json 和 apim.parameters)
- ios - 仅在 iPhone X (iOS 12) 上的横向模式下使用 UIImagePickerController 崩溃的应用程序
- excel - 使用 Pastespecial 粘贴 PDF 文件
- javascript - 从数组中仅删除一个重复项
- amazon-web-services - 使用 AWS Amplify 更新 DynamoDB 架构
- php - 如何判断自定义字段是否包含 wordpress 中的 mp4 文件?
- tableau-api - 如何为单个维度的不同元素设置不同的颜色
- python - 将持续时间创建为单独的半小时带 Pandas 日期时间