I'm getting this error: FileNotFoundError: [Errno 2] No such file or directory:
By opening a file with the name "filename.ext" using the open() function, you are using a relative path to indicate that the file is located in the current working directory.
file = open('filename.ext') //relative path
In the code snippet above, only the name of the file is provided to the open() function, which is a relative path. If the file with the given name is not present in the working directory, it will raise a "
FileNotFoundError: [Errno 2] No such file or directory" error. In such cases, using the exact or absolute path to the file can resolve the issue.
file = open(r'C:\path\to\your\filename.ext') //absolute path
The above code uses an absolute path, which contains all the information needed to locate the file, including the root directory and any subdirectories.
If the user does not provide the full path to the file (which, on Unix systems, must start with a slash), the file path in Python is interpreted relative to the current working directory. By default, the current working directory is the directory from which you started the Python program. However, in order for this to work, the directory that contains the Python executable must be included in the system's PATH environment variable, which specifies directories that are automatically searched for executables when a command is entered. Regardless, if your Python script and input file are located in different directories, you must either specify a relative path between them or use an absolute path for one of them.