【问题标题】:Can't change directories无法更改目录
【发布时间】:2019-12-06 05:42:40
【问题描述】:

我对编码非常陌生,而且我正处于“Learn Python 3 the Hard Way”一书的开头。

这本书告诉我去 Powershell 并输入 python ex1.py 以便它运行 ex1.py,但如果我这样做,它会说

C:\Users\Olga\AppData\Local\Programs\Python\Python37-32\python.exe:
can't open file 'ex1.py':
[Errno 2] No such file or directory
PS C:\Windows\system32\WindowsPowerShell\v1.0>

经过一番研究,我想到我需要将目录更改为 lpthw,这就是 ex1.py 所在的位置。

所以,我正在尝试更改目录以打开 ex1.py,当我键入 cd lpthw 时,它不会将目录更改为 lpthw,即

C:\Windows\System32\lpthw

但它会将 lpthw 添加到 powershell 所在的目录中,所以它告诉我

PS C:\Windows\system32\WindowsPowerShell\v1.0> cd lpthw
cd : Cannot find path 'C:\Windows\system32\WindowsPowerShell\v1.0\lpthw' because it does not exist.
At line:1 char:1
+ cd lpthw
+ ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Windows\syst...hell\v1.0\lpthw:String) [Set-Location], ItemNotFoundE   xception
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Windows\system32\WindowsPowerShell\v1.0>

我在网上找到了一个答案,说输入python和一个空格,然后将ex1.py拖到Powershell中,但这又给了我这个

PS C:\Windows\system32\WindowsPowerShell\v1.0> python C:\Windows\System32\lpthw\ex1.py\
C:\Users\Olga\AppData\Local\Programs\Python\Python37-32\python.exe:
can't open file 'C:\Windows\System32\lpthw\ex1.py\':
[Errno 2] No such file or directory
PS C:\Windows\system32\WindowsPowerShell\v1.0>

Soooo ...我如何让它运行 ex1.py?

谢谢!!!!

【问题讨论】:

  • 你不应该在系统目录中做任何事情。 not NOT N-O-T [grin] 切换到您的用户目录。这应该工作 >>> Set-Location -LiteralPath "$env:HOMEDRIVE$env:HOMEPATH"
  • 我这样做了,但它仍然无法运行 ex1.py。它仍然说它不存在。我也试着按照你告诉我的去做,然后说“cd lpthw”,它现在将 lpthw 附加到 PS C:\Users\Olga 上,就像 PS C:\Users\Olga\lpthw 一样,并说它不存在跨度>
  • 我建议您打开文件资源管理器并使用它来查找您需要的位置。您显然几乎是随机分散了您的文件和文件夹......找到它们并将它们移动到您可以使用它们的位置 - 在您的用户目录中。

标签: python powershell directory


【解决方案1】:

这很令人沮丧,但你会习惯的!

当我键入 cd lpthw 时,它不会将目录更改为 lpthw,而是将 lpthw 附加到 powershell 所在的目录

这些目录名称是描述计算机中某物位置的地址。在每个\ 左边的东西里面右边的东西。如果您的路径在左侧以C: 开头,经过很多目录,并以您要在最右侧使用的位置或文件结尾,那么这就是完整路径,您可以在任何地方使用它,因为它描述了一个完整的位置,没有混淆的余地。

相反,如果您只使用像“lpthw”这样的一小段路径,则它被称为相对路径,它描述了如何从当前目录开始到达某个地方。

相对路径更短、更方便,但可能会令人困惑,因为它们的最终位置会随着您的移动而改变。完整路径更费力,但更精确。

这非常接近:

python C:\Windows\System32\lpthw\ex1.py\

右侧的\ 表示ex1.py 是一个目录,我认为这是您的问题。删除它,所以它说:

python C:\Windows\System32\lpthw\ex1.py

我认为它可能会起作用。

这应该可行,但正如 Lee_Dailey cmets 所说,一个奇怪的地方; C:\Windows\System32 中的任何内容最好让 Windows 自行管理。在某些时候,尝试找到一种方法将整个 C:\Windows\System32\lpthw\ 文件夹移动到您的文档中,例如C:\Users\Olga\Documents\lpthw\

【讨论】:

  • 谢谢!在我将 lpthw 移动到 C:\Users\Olga 后,它让我切换到该目录,然后运行 ​​ex1.py
猜你喜欢
  • 2019-02-05
  • 2020-05-02
  • 2020-04-23
  • 1970-01-01
  • 2019-09-06
  • 2015-05-26
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多