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 OverflowHi,
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.
Change your import in test.py to:
from .hello import hello1
Your code is fine, I suspect your problem is how you are launching it.
You need to launch python from your '2014_07_13_test' directory.
Open up a command prompt and 'cd' into your '2014_07_13_test' directory.
For instance:
$ cd /path/to/2014_07_13_test
$ python test.py
If you cannot 'cd' into the directory like this you can add it to sys.path
In test.py:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
Or set/edit the PYTHONPATH
And all should be well...
...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'
There is a better shebang you should use.
Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.
hey guys I'm I'm trying to read in a file thats in the same folder as my script but i get an error saying it doesn't exist. any help would be greatly appriciated
Please check your current directory.
For that just do this:
import os
print (os.getcwd())
I had this same issue. I was using VS Code for my IDE, and had the folder setting to a folder above where I had the file. This obviously was the issue. Once I opened the folder in VScode where the code and the text file were both located I was able to open the file with no issues.
If you want the location of the main script, even from code that might be running in an imported module, you need to use sys.argv[0] rather than __file__. (sys.argv[0] is always the path to the main script; see http://docs.python.org/library/sys.html#sys.argv)
If you want the location of the current module, even if it's been imported by some other script, you should use __file__ as Martin says.
Here's a way to use sys.argv[0]:
import os, sys
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
print os.path.join(dirname, "b.txt")
Use the __file__ variable:
os.path.join(os.path.dirname(__file__), "b.txt")