The problem I can see here is most likely caused because you have whitespace in the script's name.
So the command line parses the name of the script up to the first word and thinks it is just Hello.
Try those two following things:
python "Hello World.py"
or
python Hello\ World.py
The quotes should "tell" the cmd that it is a single argument hence all of it is the name. Even if that doesn't work, the backslash just escapes the whitespace and that should work.
And another thing you could do is just rename the script to something like Hello_World.py - without any special symbols or whitespace.
(SOLVED)
For anyone having the same issue in the future: I got this problem from using commands at the terminal, while following the harvard CS50 course on youtube.
Whenever I tried running python file_name.py through the terminal for different files, I got the error: "can't open file 'C:\\Users\\... : [Errno 2] No such file or directory".
This came up from the code at the 5:22:15 mark, and the cowsay code at the 5:40:03 mark.
1st code:
import sys
if len(sys.argv) < 2:
print("too few arguments")
elif len(sys.argv) > 2:
print("too many arguments")
else:
print("hello, my name is ", sys.argv[1])2nd code:
import cowsay
import sys
if len(sys.argv) == 2:
cowsay.cow("hello, " + sys.argv[1])Solution: the second comment by u/aishiteruyovivi - in layman's terms: you have to change the directory from the shell's root directory to the folder wherein your file is located.
The problem I can see here is most likely caused because you have whitespace in the script's name.
So the command line parses the name of the script up to the first word and thinks it is just Hello.
Try those two following things:
python "Hello World.py"
or
python Hello\ World.py
The quotes should "tell" the cmd that it is a single argument hence all of it is the name. Even if that doesn't work, the backslash just escapes the whitespace and that should work.
And another thing you could do is just rename the script to something like Hello_World.py - without any special symbols or whitespace.
I found a solution to my problem. I found it here: https://docs.python-guide.org/starting/install3/win/#install3-windows
After I followed this guide installing chocolatey and then others it fixed my problem.
python - Can't open file 'file.py': [Errno 2] No such file or directory - Stack Overflow
FileNotFoundError: [Errno 2] No such file or directory
python - open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory' - Stack Overflow
cmd - How do I run my Python script? Why does the command line tell me "no such file or directory"? - Stack Overflow
How do I fix the Python FileNotFoundError Errno 2 No such file or directory?
What does the FileNotFoundError: [Errno 2] No such file or directory error mean?
Why am I seeing a FileNotFoundError: [Errno 2] No such file or directory error?
When you run a script from the prompt you need to use the whole path to the file (as you did when running python C:\Python37\projects\file.py) or you need to first move to the directory containing the file and then run, otherwise you rise the error you got (cause there's no file with such name in your current directory). You can move to another directory using the command cd. So what you could do if you don't want to type the path every time you run the code is:
cd C:\Python37\projects
python file.py
Open your script in another code editor like VS and check extension. It usually keeps txt behind py and that is why you get an error. Or in Options, if you use Windows, Hide extensions for known file types.
Let me clarify how Python finds files:
- An absolute path is a path that starts with your computer's root directory, for example
C:\Python\scriptsif you're on Windows. - A relative path is a path that does not start with your computer's root directory, and is instead relative to something called the working directory.
If you try to do open('recentlyUpdated.yaml'), Python will see that you are passing it a relative path, so it will search for the file inside the current working directory.
To diagnose the problem:
- Ensure the file exists (and has the right file extension): use
os.listdir()to see the list of files in the current working directory. - Ensure you're in the expected directory using
os.getcwd().
(If you launch your code from an IDE, you may be in a different directory.)
You can then either:
- Call
os.chdir(dir)wherediris the directory containing the file. This will change the current working directory. Then, open the file using just its name, e.g.open("file.txt"). - Specify an absolute path to the file in your
opencall.
By the way:
- Use a raw string (
r"") if your path uses backslashes, like so:dir = r'C:\Python32'- If you don't use raw string, you have to escape every backslash:
'C:\\User\\Bob\\...' - Forward-slashes also work on Windows
'C:/Python32'and do not need to be escaped.
- If you don't use raw string, you have to escape every backslash:
Example: Let's say file.txt is found in C:\Folder.
To open it, you can do:
os.chdir(r'C:\Folder')
open('file.txt') # relative path, looks inside the current working directory
or
open(r'C:\Folder\file.txt') # absolute path
Most likely, the problem is that you're using a relative file path to open the file, but the current working directory isn't set to what you think it is.
It's a common misconception that relative paths are relative to the location of the python script, but this is untrue. Relative file paths are always relative to the current working directory, and the current working directory doesn't have to be the location of your python script.
You have three options:
Use an absolute path to open the file:
file = open(r'C:\path\to\your\file.yaml')Generate the path to the file relative to your python script:
from pathlib import Path script_location = Path(__file__).absolute().parent file_location = script_location / 'file.yaml' file = file_location.open()(See also: How do I get the path and name of the file that is currently executing?)
Change the current working directory before opening the file:
import os os.chdir(r'C:\path\to\your\file') file = open('file.yaml')
Other common mistakes that could cause a "file not found" error include:
Accidentally using escape sequences in a file path:
path = 'C:\Users\newton\file.yaml' # Incorrect! The '\n' in 'Users\newton' is a line break character!To avoid making this mistake, remember to use raw string literals for file paths:
path = r'C:\Users\newton\file.yaml' # Correct!(See also: Windows path in Python)
Forgetting that Windows doesn't display file extensions:
Since Windows doesn't display known file extensions, sometimes when you think your file is named
file.yaml, it's actually namedfile.yaml.yaml. Double-check your file's extension.
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
I resolved this problem by navigating to C:\Python27\Scripts folder and then run file.py file instead of C:\Python27 folder
Get the directory of the file, and join it with the file you want to open:
def main():
dir_path = os.path.dirname(os.path.realpath(__file__))
lines = os.path.join(dir_path, "lines.txt")
fh = open(lines)
for line in fh.readlines():
print(line)
if __name__ == "__main__": main()
This should do the trick.
def main():
fh = open('lines.txt')
for line in fh.readlines():
print(line)
if __name__ == "__main__":
import os
curr_dir = os.path.dirname(os.path.realpath(__file__)) # get's the path of the script
os.chdir(curr_dir) # changes the current path to the path of the script
main()