FileNotFoundError even though the file exists - python

So, I was making a game using Python 3 and the Ursina game engine and keep getting this error while trying to load the file:
Traceback (most recent call last):
File "main.py", line 5, in <module>
from src.menu import *
File "/home/mysteriousk/Desktop/moni moni moni/src/menu.py", line 4, in <module>
with open('../assets/database/database.json','r') as E:
FileNotFoundError: [Errno 2] No such file or directory: '../assets/database/database.json'
The file I am executing is main.py and the file I am importing is in the folder src and is named menu.py`. The code for loading my file:
with open('../assets/database/database.json','r') as E:
stuffs = json.load(E)
The file obviously exists in the directory assets/database. Please let me know whats wrong any help is appreciated.

Related

OSError: file does not exist using ytree

I've got some data I need to load using ytree which can be done via:
import ytree
a = ytree.load('ctrees.h5')
However, doing this gives the following error:
OSError: file does not exist: ctrees.h5.
With traceback:
Traceback (most recent call last):
File "C:\Users\Documents\untitled1.py", line 3, in <module>
a = ytree.load('ctrees.h5')
File "C:\Users\anaconda3\lib\site-packages\ytree\data_structures\arbor.py", line 1089, in load
raise IOError("file does not exist: %s." % filename)
OSError: file does not exist: ctrees.h5.
The same happens if I try a = ytree.load("ctrees.h5"). I can see that the file exists, I have changed the working directory to the correct one and my script is saved in the same directory. I also tried specifying the full path to the file with no joy.
I'm using Python 3.7 and ytree 2.3 on Windows 10
Any help appreciated.
EDIT (RESOLVED):
Fixed the issue by specifying path at top of script and reading file as:
PATH='C:\\path\\to\\file\\directory\\'
a = ytree.load(PATH + "ctrees.h5")

Reading File function in python

hello I am trying to apply the file reading function in python
f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
print(f.read())
It keeps giving me the error of
Traceback (most recent call last):
File "main.py", line 1, in <module>
f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\hamza\\Desktop\\family.txt'
please help me to resolve this issue
Thankyou
That is the correct code to read a file. C:\\Users\\hamza\\Desktop\\family.txt" must not exist.
You can try following code also,
path="C:\\Users\\hamza\\Desktop\\family.txt"
f = open(path,"r")
print(f.read())
But your code is also correct.
Please check if file is present on desktop and name of file is correct or not

Error message when using openpyxl.load_workbook() function

I have just started to learn programming and I am currently trying to read an excel file from IDLE. I'm following instruction from the book "Automate the Boring Stuff". I have successfully imported openpyxl, and thereafter, as instructed tried wb = openpyxl.load_workbook('example.xlsx') where I exchanged "example" to the actual name of the workbook. However, I get this error message:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 117, in load_workbook
archive = ZipFile(filename, 'r', ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1216, in __init__
self.fp = io.open(file, filemode) FileNotFoundError: [Errno 2] No such file or directory: 'jan.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
wb = openpyxl.load_workbook('jan.xlsx')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 145, in load_workbook
raise InvalidFileException(unicode(e))
openpyxl.exceptions.InvalidFileException: [Errno 2] No such file or directory: 'jan.xlsx'
I don't understand how to solve this.
This error message simply says that python can't locate the file. When you try to open a file 'jan.xlsx', it's trying to locate it in the base directory of your code in your IDE. So say your code is in a directory called /Users/username/PycharmProjects/myCode
(I'm assuming here you are on a Mac OS as the path to your python suggests...
but jan.xlsx is in /Users/username
Since that is 2 directories up from your code directory, you can do one of two things:
Write in the absolute path to the file:
wb = openpyxl.load_workbook('/Users/username/jan.xlsx')
Use a relative path that is relative to the base project directory. Two dots in a relative path means one level up from the current directory. So if the excel file is 2 levels up, you can do:
wb = openpyxl.load_workbook('../../jan.xlsx')

IOError: [Errno 2] No such file or directory: '../data/mnist.pkl.gz'

I'm new to Python and self-teaching myself neural networks from this http://neuralnetworksanddeeplearning.com/chap1.html and I'm having trouble importing the files that I've downloaded for the exercises. This is the error message I keep getting:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
File "mnist_loader.py", line 68, in load_data_wrapper
File "mnist_loader.py", line 42, in load_data
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.py", line 34, in open
return GzipFile(filename, mode, compresslevel)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.py", line 94, in __init__
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: '../data/mnist.pkl.gz'
I've looked around and tried this:
>>> open('Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl')
but get this error:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
open('Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl')
IOError: [Errno 2] No such file or directory: 'Users/bryanjordan/Documents/neural-networks-and-deep-learning-master/mnist.pkl'
I also meet this question. And I find the solution to solve this problem.
If you run the code in the python console, please first use "cd" into the "/src" folder, and run the code.
If you run the code in a python file (like me), you should put the file into the "/src" folder.
It will be solved. The only reason is that the relative path. You should stand in the right place.
Hope to help you.

How to open files, web browsers, and URLs in Python, not in IDLE.

I know you can open files, browsers, and URLs in the Python GUI. However, I don't know how to apply this to programs. For example, none of the below work. (The below are snippets from my growing chat bot program):
def browser():
print('OPENING FIREFOX...')
handle = webbroswer.get() # webbrowser is imported at the top of the file
handle.open('http://youtube.com')
handle.open_new_tab('http://google.com')
and
def file():
file = str(input('ENTER THE FILE\'S NAME AND EXTENSION:'))
action = open(file, 'r')
actionTwo = action.read()
print (actionTwo)
These errors occur, in respect to the above order, but in individual runs:
OPENING FIREFOX...
Traceback (most recent call last):
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 202, in <module>
askForQuestions()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 64, in askForQuestions
browser()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 38, in browser
handle = webbroswer.get()
NameError: global name 'webbroswer' is not defined
>>>
ENTER THE FILE'S NAME AND EXTENSION:file.txt
Traceback (most recent call last):
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 202, in <module>
askForQuestions()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 66, in askForQuestions
file()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 51, in file
action = open(file, 'r')
IOError: [Errno 2] No such file or directory: 'file.txt'
>>>
Am I handling this wrong, or can I just not use open() and webbrowser in a program?
You should read the errors and try to understand them - they are very helpful in this case - as they often are:
The first one says NameError: global name 'webbroswer' is not defined.
You can see here that webbrowser is spelled wrong in the code. It also tells you the line it finds the error (line 38)
The second one IOError: [Errno 2] No such file or directory: 'file.txt' tells you that you're trying to open a file that doesn't exist. This does not work because you specified
action = open(file, 'r')
which means that you're trying to read a file. Python does not allow reading from a file that does not exist.

Categories

Resources