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.

Answer from Fanatique on Stack Exchange
🌐
Reddit
reddit.com › r/learnpython › file location error - [errno 2] no such file or directory. how to fix?
r/learnpython on Reddit: file location error - [Errno 2] No such file or directory. how to fix?
August 20, 2024 -

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

Discussions

python - Can't open file 'file.py': [Errno 2] No such file or directory - Stack Overflow
I'm using Windows10 and wants to run my python script by cmd The command: python file.py return the message: python: can't open file 'file.py': [Errno 2] No such file or directory but when I ... More on stackoverflow.com
🌐 stackoverflow.com
FileNotFoundError: [Errno 2] No such file or directory
Hi, I am trying to import Excel file in Python using Pandas. I am working in Spyder. Below code is copied from a website and edited to suit my requirement. import pandas as pd excel_file = ‘ExcelPandasPythonExample.x… More on discuss.python.org
🌐 discuss.python.org
8
1
February 28, 2020
python - open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory' - Stack Overflow
See How should I write a Windows path in a Python string literal?. When trying to create a new file using a file mode like w, the path to the new file still needs to exist - i.e., all the intervening folders. See for example Trying to use open(filename, 'w' ) gives IOError: [Errno 2] No such file or directory ... More on stackoverflow.com
🌐 stackoverflow.com
cmd - How do I run my Python script? Why does the command line tell me "no such file or directory"? - Stack Overflow
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? More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I fix the Python FileNotFoundError Errno 2 No such file or directory?
To fix FileNotFoundError: [Errno 2] No such file or directory error, check that you are referring to the right file or folder in your program. Here is an example scenario featuring a FileNotFoundError message. An Example Scenario We’re writing a program that lists all the files in a folder. The folder we are referencing contains a list of markdown documentation for our project. To start, let’s import the os library, which has a method that lets us see all the files in a folder: import os Next, we are going to use the os.listdir() method to get a list of the files in our folder: for f in os.
🌐
careerkarma.com
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory ...
What does the FileNotFoundError: [Errno 2] No such file or directory error mean?
The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.
🌐
careerkarma.com
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory ...
Why am I seeing a FileNotFoundError: [Errno 2] No such file or directory error?
Any message with the contents FileNotFoundError indicates that Python cannot find the file you are referencing. Python raises this error because your program cannot continue running without being able to access the file to which your program refers. This error is usually raised when you use the os library. You will see an IOError if you try to read or write to a file that does not exist using an open() statement.
🌐
careerkarma.com
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory ...
🌐
Python.org
discuss.python.org › python help
Directory not found - Python Help - Discussions on Python.org
March 11, 2023 - I am getting this error: `C:\Users\Anthony.DESKTOP-ES5HL78\AppData\Local\Programs\Python\Python310\python.exe: can’t open file ‘C:\Users\Anthony.DESKTOP-ES5HL78\04_02.py’: [Errno 2] No such file or directory’ I am sure …
🌐
CareerKarma
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory Solution Wiki | Career Karma
To fix this error, check that you are referring to the right file or folder in your program. ... Any message with the contents FileNotFoundError indicates that Python cannot find the file you are referencing.
🌐
Python.org
discuss.python.org › python help
FileNotFoundError: [Errno 2] No such file or directory - Python Help - Discussions on Python.org
February 28, 2020 - Hi, I am trying to import Excel file in Python using Pandas. I am working in Spyder. Below code is copied from a website and edited to suit my requirement. import pandas as pd excel_file = ‘ExcelPandasPythonExample.xls’ MyVariable = pd.read_excel(excel_file) MyVariable.head() I am getting below error while trying this code.
Find elsewhere
🌐
CodeWithHarry
codewithharry.com › blogpost › solving-python-error-errno-2-no-such-file-or-directory
[Solved] FileNotFoundError: [Errno 2] No such file or directory: 'harry.png' | Blog | CodeWithHarry
This error occurs when Python tries to open a file that does not exist at the specified path. The error message is quite descriptive and pinpoints the problem precisely. The [Errno 2] part of the error refers to a specific error code in the underlying operating system that translates to "No ...
🌐
SQLPad
sqlpad.io › home › blog › fixing 'no such file or directory' error in python
Fixing 'No Such File or Directory' Error in Python | SQLPad
April 29, 2024 - Here's how to do it in Python: Try-Except Blocks: The most straightforward way to handle errors. Wrap the file access code in a try-except block, catching FileNotFoundError to handle files that don't exist gracefully.
Top answer
1 of 13
174

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\scripts if 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) where dir is 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 open call.

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.

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
2 of 13
41

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 named file.yaml.yaml. Double-check your file's extension.

🌐
Bobby Hadz
bobbyhadz.com › blog › python-filenotfounderror-no-such-file-or-directory
FileNotFoundError: [Errno 2] No such file or directory [Fix] | bobbyhadz
April 8, 2024 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › filenotfounderror-errno-2-no-such-file-or-directory-in-python
Filenotfounderror: Errno 2 No Such File Or Directory in Python - GeeksforGeeks
July 23, 2025 - The FileNotFoundError with Errno 2: No such file or directory is a Python exception that occurs when a file or directory is referenced in code, but the interpreter cannot locate it at the specified location.
🌐
PythonAnywhere
help.pythonanywhere.com › pages › NoSuchFileOrDirectory
No such file or Directory? | PythonAnywhere Help
December 17, 2016 - One common reason for these kinds of errors is that your working directory settings might be different on PythonAnywhere from your own machine.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 11602067518226-C-Program-Files-Python311-python-exe-can-t-open-file-C-Program-Errno-2-No-such-file-or-directory
C:\Program Files\Python311\python.exe: can't open file 'C:\\Program': [Errno 2] No such file or directory – IDEs Support (IntelliJ Platform) | JetBrains
May 22, 2023 - I reinstalled my pycharm this version to space less path “C:\Users\syedh\PyCharm Community Edition 2023.1.2” Still getting same error. What am i missing? I am JS developer, start python flask project 1 week ago. ... Hasanbadsha creating a directory junction to the source of PyCharm worked out for me.
🌐
OneUptime
oneuptime.com › home › blog › how to fix 'filenotfounderror' in python
How to Fix 'FileNotFoundError' in Python
January 25, 2026 - This guide covers common causes ... file = open("data/config.json") # Looks in current working directory # Solution 1: Use absolute path file = open("/home/...
🌐
Medium
medium.com › @teamcode20233 › solving-python-environment-error-no-such-file-or-directory-61a1478e4b77
Solving Python Environment Error: No Such File or Directory | by Teamcode | Medium
July 7, 2023 - The “env: python: no such file or directory” error occurs when the Python program is unable to access the specified file or directory due to insufficient permissions. It can happen when trying to open a file for reading or writing, executing ...
🌐
GitHub
github.com › python › pythondotorg › issues › 1381
Python: can't open file No such file or directory · Issue #1381 · python/pythondotorg
January 23, 2019 - this suddenly appeared in my terminal python: can't open file 'C:\Users\New User\Anaconda3\Library\bin....\bin\conda': [Errno 2] No such file or directory
Author: python
🌐
Reddit
reddit.com › r/vscode › c:\python\python.exe: can't open file [errno 2] no such file or directory
r/vscode on Reddit: C:\Python\python.exe: can't open file [Errno 2] No such file or directory
March 22, 2023 - I get this issue in every single python code that i try to run in vs code. I hope this screen shot is enough ... Looks like you need to check your path. Find the directory where the file is and open VS Code in that directory.