如何更改 Powershell 中的路径?

use*_*568 2 python powershell notepad++

Powershell斗争-_-

大家好!我最近买了一本关于Python的教科书。我目前正在使用 Notepad++ 作为编辑器。我正在尝试使用 Powershell 打开我的 ex1.py 文件(位于桌面上)。例如,我尝试使用“python ex1.py”命令在 Powershell 上打开我的文本文件。每次当我尝试使用“cd C:\Desktop”更改工作目录时,我都会收到上述错误。

Jen*_*nsG 6

解决方案

您正在寻找的命令是

set-location $env:userprofile\desktop
Run Code Online (Sandbox Code Playgroud)

或短

sl $env:userprofile\desktop    # sl is an alias for set-location
Run Code Online (Sandbox Code Playgroud)

甚至

cd $env:userprofile\desktop    # cd is another alias for set-location
Run Code Online (Sandbox Code Playgroud)

解释

与许多其他系统路径一样,用户的桌面路径取决于您所使用的 Windows 操作系统的确切版本。这些路径已被重命名并在不同的操作系统版本之间移动了很多,因此使用环境变量来定位当前用户的主目录是最安全的选择。PowerShellenv:驱动器允许访问这些定义:

尝试在 PoSh 控制台中输入以下内容:

get-childitem env:   # gives a list of all environment variables defined 
ls env:              # same, via alias
dir env:             # same, via alias

dir $env:userprofile   # shows contents of the current users (your) home directory
Run Code Online (Sandbox Code Playgroud)

Desktop文件夹位于该主文件夹中,因此:

set-location $env:userprofile\desktop
Run Code Online (Sandbox Code Playgroud)

会将当前位置设置为所需的文件夹,并且将以可移植的方式执行此操作。