如何用python打开Powershell到它的默认位置?

问题描述

我正在尝试制作一个可以打开所有应用程序的数字助理。当我打开 PowerShell 时,目录被设置为 python 脚本所在的位置。我都知道如何打开命令行到正确的文件夹我只需要PowerShell。

我在用什么

  • Windows 10
  • Python 3.7.9
  • 操作系统模块
  • Thonny ide 3.3.6

我想做的是打开 PowerShell 以

C:\Users\MyUsername>

我尝试过的 我在 Powershell 中使用命令“get-StartApps”来获取所有应用程序 ID

这是我试图用来打开 PowerShell

import os
os.system('start Powershell ')

os.startfile(r'C//Users//'+str(os.path.expandvars("%userprofile%"))+'//AppData//Roaming//Microsoft//Windows//Start Menu//Programs//Windows PowerShell//Windows PowerShell.lnk')

我不确定是否所有计算机上的应用程序 ID 都相同,所以我仅举个例子

import os
os.system('start explorer shell:appsfolder\{Id number}\WindowsPowerShell\v1.0\powershell.exe')

只需打开文件资源管理器,但可以打开其他应用程序。

另外,我希望它适用于所有机器,所以如果你知道如何请告诉我。

解决方法

试试这个:

os.system('start /D "C:/Users" powershell')

启动命令的 /D 开关设置了 powershell 打开的路径。

,
os.system('start /D'+ str(os.path.expandvars("%userprofile%"))+' powershell')