Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial
  1. Python Introduction
  2. Python Fundamentals
  3. Python Control Flow
  4. Python Data Structures
  5. Python Files
  6. Python Essentials
  7. Python Exercises
  8. Python Networking
  9. Python Programming
  10. Python Exceptions
  11. Interview Questions

FileNotFoundError: [Errno 2] No such file or directory

By:Rajesh P.S.

When youopen a file with the name "filename.ext"; you are telling the open() function that your file is in thecurrent working directory . This is called a relative path.

file = open('filename.ext') //relative path

In the above code, you are not giving the full path to a file to the open() function, just its name - a relative path. 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.

file = open(r'C:\path\to\your\filename.ext') //absolute path

In the above code, all of the information needed to locate the file is contained in the path string - absolute path.

It's a common misconception thatrelative path is relative to the location of the python script, but this is not true. Relative file paths are always relative to the current working directory, and the current working directory doesn't have to be the location of yourpython script .

Other reasons?

There are several other reasons why the FileNotFoundError Errno 2 No such file or directory error can occur:

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

  1. Accidentally using escape sequences in a file path
path = 'C:\Users\neo\filename.ext'

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.

path = r'C:\Users\neo\filename.ext'
  1. 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?

  1. Make sure the file exists

Use os.listdir() to see the list of files in the current working directory.

  1. Use an absolute path to open the file
file = open(r'C:\path\to\your\filename.ext') //absolute path
  1. Raw String Literals

Remember to use a raw string literals if your path uses backslashes.

dir = r'C:\path\to\your\filename.ext'
  1. Change the current working directory before opening the file
import osos.chdir(r'C:\path\to\your\file')file = open('filename.ext')


python file not found error

Relative Path Vs. Absolute Path

A 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 calledabsolute 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 calledrelative 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.




Next >  Fatal error: Python.h: No such file or directory


  1. TypeError: 'NoneType' object is not subscriptable
  2. IndexError: string index out of range
  3. IndentationError: unexpected indent Error
  4. ValueError: too many values to unpack (expected 2)
  1. SyntaxError- EOL while scanning string literal
  2. TypeError: Can't convert 'int' object to str implicitly
  3. IndentationError: expected an indented block
  4. ValueError: invalid literal for int() with base 10
  5. IndexError: list index out of range : Python
  6. AttributeError: 'module' object has no attribute 'main'
  7. UnboundLocalError: local variable referenced before assignment
  8. TypeError: string indices must be integers
  9. Fatal error: Python.h: No such file or directory
  10. ZeroDivisionError: division by zero
  11. ImportError: No module named requests | Python
  12. TypeError: 'NoneType' object is not iterable
  13. SyntaxError: unexpected EOF while parsing | Python
  14. zsh: command not found: python
  15. Unicodeescape codec can't decode bytes in position 2-3
  16. The TypeError: 'tuple' object does not support item assignment
  17. The AttributeError: 'bytes' object has no attribute 'read'





[8]ページ先頭

©2009-2025 Movatter.jp