Python 101:无法打开文件:没有那个文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34109771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 101: Can't open file: No such file or directory
提问by Szumilo
here's a quick background: I am on day 1 of learning Python. No prior coding experience. Running Windows 8. Python 2.7 installed (location: C:\Python27). I have added the path "C:\Python27\;" to the environment variables and '.py:' to PATHEXT. I am able to launch Python.
这是一个快速的背景:我正在学习 Python 的第一天。没有编码经验。运行 Windows 8。已安装 Python 2.7(位置:C:\Python27)。我添加了路径“C:\Python27\;” 到环境变量和 '.py:' 到 PATHEXT。我能够启动 Python。
To learn, I'm starting with the Google Developers course on Python. I've downloaded the google-python-exercises file to my desktop which has a file 'hello.py'. The Google page(https://developers.google.com/edu/python/set-up) says I should be able to enter 'python hello.py' in the command prompt and it should print 'Hello World'.
为了学习,我从关于 Python 的 Google Developers 课程开始。我已将 google-python-exercises 文件下载到我的桌面,其中包含一个文件“hello.py”。Google 页面(https://developers.google.com/edu/python/set-up)说我应该能够在命令提示符中输入“python hello.py”,它应该打印“Hello World”。
When I attempt this, I get the message: python: can't open file 'hello.py: [Errno 2] No such file or directory
. How can I fix this?
当我尝试这样做时,我收到消息:python: can't open file 'hello.py: [Errno 2] No such file or directory
。我怎样才能解决这个问题?
Update
更新
I'm able to run it by running cmd from the folder. However, this seems to be a temporary solution. How do I program Python to take a new folder (C:\Python27\MyProjects) and search it? It sounds like I need to add/edit a directory, but I'm not sure how to do this.
我可以通过从文件夹中运行 cmd 来运行它。然而,这似乎是一个临时解决方案。如何编写 Python 以获取新文件夹 (C:\Python27\MyProjects) 并对其进行搜索?听起来我需要添加/编辑目录,但我不确定如何执行此操作。
回答by ilyas patanam
Prior to running python, type cd
in the commmand line, and it will tell you the directory you are currently in. When python runs, it can only access files in this directory. hello.py
needs to be in this directory, so you can move hello.py
from its existing location to this folder as you would move any other file in Windows or you can change directories and run python in the directory hello.py
is.
在运行python之前,cd
在命令行中输入,它会告诉你当前所在的目录。python运行时,它只能访问该目录下的文件。hello.py
需要在此目录中,因此您可以hello.py
像移动 Windows 中的任何其他文件一样从其现有位置移动到此文件夹,或者您可以更改目录并在该目录中运行 python hello.py
。
Edit: Python cannot access the files in the subdirectory unless a path to it provided. You can access files in any directory by providing the path. python C:\Python27\Projects\hello.p
编辑:除非提供了路径,否则 Python 无法访问子目录中的文件。您可以通过提供路径访问任何目录中的文件。python C:\Python27\Projects\hello.p
回答by freeislandguy
From your question, you are running python2.7 and Cygwin.
根据您的问题,您正在运行 python2.7 和 Cygwin。
Python should be installed for windows, which from your question it seems it is. If "which python" prints out /usr/bin/python , then from the bash prompt you are running the cygwin version.
应该为 Windows 安装 Python,从您的问题来看似乎是这样。如果“which python”打印出 /usr/bin/python ,那么从 bash 提示符您正在运行 cygwin 版本。
Set the Python Environmental variables appropriately , for instance in my case:
适当地设置 Python 环境变量,例如在我的情况下:
PY_HOME=C:\opt\Python27
PYTHONPATH=C:\opt\Python27;c:\opt\Python27\Lib
In that case run cygwin setup and uninstall everything python. After that run "which pydoc", if it shows
在这种情况下,运行 cygwin setup 并卸载所有 python。之后运行“which pydoc”,如果它显示
/usr/bin/pydoc
Replace /usr/bin/pydoc with
将 /usr/bin/pydoc 替换为
#! /bin/bash
/cygdrive/c/WINDOWS/system32/cmd /c %PYTHONHOME%\Scripts\pydoc.bat
Then add this to $PY_HOME/Scripts/pydoc.bat
然后将其添加到$PY_HOME/Scripts/pydoc.bat
rem wrapper for pydoc on Win32
@python c:\opt\Python27\Lib\pydoc.py %*
Now when you type in the cygwin bash prompt you should see:
现在,当您输入 cygwin bash 提示符时,您应该看到:
$ pydoc
pydoc - the Python documentation tool
pydoc.py <name> ...
Show text documentation on something. <name>
may be the name of a Python keyword, topic,
function, module, or package, or a dotted
reference to a class or function within a
module or module in a package.
...
回答by Nguyet Tran
I resolved this problem by navigating to C:\Python27\Scripts folder and then run file.py file instead of C:\Python27 folder
我通过导航到 C:\Python27\Scripts 文件夹然后运行 file.py 文件而不是 C:\Python27 文件夹解决了这个问题
回答by Diya
Try uninstalling Python and then install it again, but this time make sure that the option Add Python to Path is marked as checked during the installation process.
尝试卸载 Python,然后重新安装,但这次请确保在安装过程中将选项 Add Python to Path 标记为已选中。