如何在Powershell中更改路径?
大家好!我最近买了一本关于Python的教科书。目前我在用Notepad++作为编辑器。我想通过Powershell打开我桌面上的ex1.py文件。比如,我想用“python ex1.py”这个命令在Powershell中打开我的文本文件。每次我尝试用“cd C:\Desktop”来改变我的工作目录时,都会出现上面的错误。
2 个回答
-1
试着输入这个:
cd "C:/Documents and Settings/YOURUSERNAME/Desktop"
作为普通用户,你的桌面文件夹可能不在C:/这个位置。
6
解决方案
你需要的命令是
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:
驱动器可以让你访问这些定义:
试着在PowerShell控制台中输入这个:
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
这会把当前的位置设置到你想要的文件夹,而且这样做是很灵活的。