Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 101: Can't open file: No such file or directory

I have Python 2.7 installed at C:\Python27 and I have added the path C:\Python27\; to the environment variables and .py: to PATHEXT. I am able to launch Python.

I downloaded a folder google-python-exercises to my desktop, which contains a script hello.py.

Following the advice in the Google Developers course, I try to run the script by using python hello.py at the command prompt.

When I attempt this, I get the message: python: can't open file 'hello.py: [Errno 2] No such file or directory. What is wrong, and how am I supposed to fix it? I found that I can solve the problem by running cmd from the folder, but this seems like a temporary solution.

like image 584
Szumilo Avatar asked Dec 05 '15 19:12

Szumilo


People also ask

How do I fix Python error No such file or directory?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

How do I fix No such file or directory?

In some cases, this error could be shown when the path of the specified file or folders exceeds 258 characters in length. The way to solve this is to reduce the length of the full path to the items specified, either by moving or renaming the file(s) and/or containing folders.

Can't open a file in Python?

To fix the “python: can't open file” error, you need to go into the folder where manage.py is located. To do so, you need to type “cd mysite” at the terminal followed by “python manage.py runserver”.

Why does it say no such file or directory?

The error "FileNotFoundError: [Errno 2] No such file or directory" is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path. In the above code, all of the information needed to locate the file is contained in the path string - absolute path.


2 Answers

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.py

like image 152
ilyas patanam Avatar answered Oct 11 '22 07:10

ilyas patanam


I resolved this problem by navigating to C:\Python27\Scripts folder and then run file.py file instead of C:\Python27 folder

like image 42
Nguyet Tran Avatar answered Oct 07 '22 17:10

Nguyet Tran