FileNotFoundError: [Errno 2] No such file or directoryWhen you open a file with the name "filename.ext"; you are telling the open() function that your file is in the current working directory . This is called a relative path.
There are several other reasons why the FileNotFoundError Errno 2 No such file or directory error can occur:
- Misspelled filename
There may be times when your filename will have been misspelled. In such a case, the file you specified will not exist in the current directory. So, recheck your filename.
- Accidentally using escape sequences in a file path
Above code throws error because the '\n' in 'Users\neo' is a line break character.
To avoid making this mistake, remember to use raw string literals for file paths.
- Forgetting that Windows doesn't display file extensions
Since Windows doesn't display known file extensions, sometimes when you think your file is named "myFile.yaml", it's actually named "myFile.yaml.yaml". So, double-check your file's extension.
How to avoid FileNotFoundError: [Errno 2] No such file or directory?
- Make sure the file exists
Use os.listdir() to see the list of files in the current working directory.
- Use an absolute path to open the file
- Raw String Literals
Remember to use a raw string literals if your path uses backslashes.
- Change the current working directory before opening the file
Relative Path Vs. Absolute PathA file is identified by its path through the file system. A path is either relative or absolute. The path with reference to root directory is called absolute path . An absolute path always contains the root element and the complete directory list required to locate the file. For example: "C:\path\to\your\filename.ext". All of the information needed to locate the file is contained in the path string. The path with reference to current directory is called relative path . A relative path needs to be combined with another path in order to access a file. For example: "your\filename.ext" is a relative path. Without more information, a program cannot reliably locate the joe/foo directory in the file system.
- TypeError: 'NoneType' object is not subscriptable
- IndexError: string index out of range
- IndentationError: unexpected indent Error
- ValueError: too many values to unpack (expected 2)
- SyntaxError- EOL while scanning string literal
- TypeError: Can't convert 'int' object to str implicitly
- IndentationError: expected an indented block
- ValueError: invalid literal for int() with base 10
- IndexError: list index out of range : Python
- AttributeError: 'module' object has no attribute 'main'
- UnboundLocalError: local variable referenced before assignment
- TypeError: string indices must be integers
- Fatal error: Python.h: No such file or directory
- ZeroDivisionError: division by zero
- ImportError: No module named requests | Python
- TypeError: 'NoneType' object is not iterable
- SyntaxError: unexpected EOF while parsing | Python
- zsh: command not found: python
- Unicodeescape codec can't decode bytes in position 2-3