The working directory is not set to the directory of the script, but to the current directory where you started the script.

Use __file__ to determine the file location and use that as a starting point to make filename an absolute path:

import os

here = os.path.dirname(os.path.abspath(__file__))

filename = os.path.join(here, '20140210204804.kml')
Answer from Martijn Pieters on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › "file not found" error, but both files are in the same directory.
r/learnpython on Reddit: "File not found" error, but both files are in the same directory.
September 15, 2023 -

Hi,

I'm working on an exercise from Python Crash Course, and I'm getting this error, even though both my program file and the read file are in the same directory/folder: FileNotFoundError: [Errno 2] No such file or directory: 'learning_python.txt'

This is the code i'm running:

from pathlib import Path

print("--- Reading in the entire file:")

path = Path('learning_python.txt')

contents = path.read_text()

print(contents)

print("\n--- Looping over the lines:")

lines = contents.splitlines()

for line in lines:

print(line)

I'm only getting the error when I run the program on VS Code, when I run it in Sublime Text the program runs with no issues.

Does anyone have any suggestions on what might be the problem and how to fix it?

Thanks in advance.

🌐
Reddit
reddit.com › r/learnpython › python can't find file in the same directory
python can't find file in the same directory : r/learnpython
May 19, 2021 - Non-absolute paths are relative to the working directory, not the file location. ... What apps can i use to write python?
🌐
YouTube
youtube.com › codinggpt
python can t find file in same directory - YouTube
Download this code from https://codegive.com In Python, it's common to encounter situations where the interpreter cannot find a file specified in your code. ...
Published: December 27, 2023
Views: 316
🌐
McNeel Forum
discourse.mcneel.com › scripting
Python not able to find existing file? - Scripting - McNeel Forum
January 6, 2021 - Hello all, I have a whole load of files where a particular layer has to be deleted and placed into a new folder as a DXF. The odd thing is I had it working perfectly earlier however when I went back to run it I noticed the command line was full of errors saying the file could not be found, but stranger than that, a copy of all my files were made in the destination folder and the desired files with the removed layers were left in the origin folder.
🌐
Quora
quora.com › Why-can-I-not-open-the-text-file-though-its-in-the-same-directory-with-my-Python-file
Why can I not open the text file though it's in the same directory with my Python file? - Quora
Answer (1 of 6): Have you make sure that * Your filename is correct. It is case-sensitive. Abcd.txt is not the same file as abcd.txt. * Permission is correct? * Working directory is correct? It is easier to troubleshoot if you have error code or message. E.g. access denied must have been abou...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 71468831 › python-unable-to-find-file-in-same-directory › 71468946
json - python unable to find file in same directory - Stack Overflow
... That is very strange that it was working previously and has now begun to show the error. Usually the file not found error is due to the path being incorrect. But as you have shown it looks correct.
🌐
Python.org
discuss.python.org › python help
My program is not finding another file - Python Help - Discussions on Python.org
May 13, 2024 - The following code has a problem: #! /usr/bin/env python3 # -*- coding: utf-8 -*- # # GUI module generated by PAGE version 8.0 # in conjunction with Tcl version 8.6 # May 11, 2024 01:17:29 PM BST platform: Darwin import sys import tkinter as tk import tkinter.ttk as ttk from tkinter.constants import * import os.path _location = os.path.dirname(__file__) import LoginForm_support The last line is the problem.
🌐
GitHub
github.com › microsoft › pylance-release › issues › 4861
Can't import file from the same directory · Issue #4861 · microsoft/pylance-release
September 21, 2023 - Python version (and distribution if applicable, e.g. Anaconda): 3.10 ... This behavior start from version 2023.9.11, can be fixed by revert to version 9.10 or add current directory to python.analysis.extraPaths
Author: microsoft
🌐
PythonAnywhere
pythonanywhere.com › forums › topic › 31924
"ImportError:"... files in the same directory not finding said-file. : Forums : PythonAnywhere
Initially, I had a folder named "classes", which of course contained a class I need to use in my actual script file. It kept telling me that there is no module found. Then I put that class file in the same directory where the script file is, and now it is telling me "ImportError: attempted relative import with no known parent package".
🌐
Reddit
reddit.com › r/learnpython › python can't find folder in the same directory
r/learnpython on Reddit: Python Can't Find Folder in the same directory
November 15, 2021 - I haven't used Spyder, but IDEs will usually execute scripts from the current project's base directory. If you want to open a file that's next to your script, no matter where you execute it from, you can always base the path off __file__, which contains the path to the current script:
🌐
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 …
🌐
Quora
quora.com › Why-cant-Python-find-a-file
Why can't Python find a file? - Quora
Answer (1 of 4): First of all, remove words from quotes. It should be open(words,"r"), because saying "words" means it should open the file named "words" with no extensions which would obviously be an error. The variable words will be represented without quotes. Also, once you do that, keep in m...
🌐
Python.org
discuss.python.org › python help
Cannot find file using pathlib - Python Help - Discussions on Python.org
April 8, 2023 - Hi there, thanks in advance for trying to help! I’m currently learning Python using Eric Matthes’ “Python Crash Course”. I’m stuck at trying to read a file using pathlib. Here’s the code in question: from pathlib import Path path = Path('pi_digits.txt') contents = path.read_text() print(contents) I then get the following error message: PS C:\Users\aleks\Desktop\python_work> & C:/Users/aleks/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/aleks/Desktop/python_work/Chapter 10/fi...