Please check your current directory.
For that just do this:
import os
print (os.getcwd())
Answer from Roman Belousov on Stack OverflowPlease 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.
Python script can't find file, even though they are in the same folder?
Python can't find module in the same folder - Stack Overflow
Python not finding file on local desktop - Stack Overflow
Python doesn't find local module although it is installed and its location exists in sys.path
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
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.
Try to use os.path.expanduser to get the correct path to the desktop
if(os.path.exists(os.path.expanduser('~/Desktop/test.csv')):
print "Found"
else:
print "NOT Found"
import os
if(os.path.exists('C:\\Users\\user\\Desktop\\test.csv')) == True:
print("Found")
else:
print("NOT Found")
you can also do it like this, 'C:/Users/user/Desktop/test.csv' which is a little neater
edited: the destination was incorrect
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.
Try pathlib, it'll help with trouble shooting. Note the forward slashes in this example. That'll work with open() as well.
from pathlib import Path
p = Path('E:/all_study/python/Bearing_fault.mat')
print(p.exists())
print(p.is_file())
print(p.read_text())
with p.open() as f:
f.read_line()
Alternatively, prefixing the string with r disables escaping allowing direct pasting of the path.
p = Path(r'E:\all_study\python\Bearing_fault.mat')
But here's the main reason for pathlib. If the file is in the same directory as the script, leverage that to improve portability and reliability. (Could easily be adapted to support scenario where the file is in a different directory to python script)
p = Path(__file__).with_name('Bearing_fault.mat')
>>> print("\E:\all_study\python\Bearing_fault.mat")
\E:ll_study\python\Bearing_fault.mat
It's no surprise that path doesn't exist! Every backslash in the string literal is being parsed for escape sequences, such as \a for alert which produces a bell character. In addition, you don't put backslashes in front of drive letters. You can disable the escape sequences for a particular literal using raw strings:
>>> print(r"E:\all_study\python\Bearing_fault.mat")
E:\all_study\python\Bearing_fault.mat
Hello, I created a EXE using pyinstaller from within the terminal in pycharm. Video on how I did this here: https://youtu.be/bqNvkAfTvIc
My error message is in comments. Can anyone help to troubleshoot why my exe is not finding my input file that exists in the folder?