gpt4 book ai didi

python - 如何更改 Powershell 中的路径?

转载 作者:行者123 更新时间:2023-12-02 04:29:45 25 4
gpt4 key购买 nike

Powershell Struggle -_-

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

最佳答案

解决方案

您正在寻找的命令是

set-location $env:userprofile\desktop

或简短

sl $env:userprofile\desktop    # sl is an alias for set-location

甚至

cd $env:userprofile\desktop    # cd is another alias for set-location

说明

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

尝试在 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

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

set-location $env:userprofile\desktop

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

关于python - 如何更改 Powershell 中的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24108324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com