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')
Related
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.
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")
I know there are questions like that but I still wanted to ask this because I couldn't solve, so there is my code:
#! python3
import os
my_path = 'E:\\Movies'
for folder in os.listdir(my_path):
size = os.path.getsize(folder)
print(f'size of the {folder} is: {size} ')
And I got this error:
Traceback (most recent call last):
File "c:/Users/ataba/OneDrive/Masaüstü/Programming/python/findingfiles.py", line 7, in <module>
size = os.path.getsize(folder)
File "C:\Program Files\Python37\lib\genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'FordvFerrari1080p'
When I write print(folder) instead of getting their size it shows the folders so I don't think the program can't find them.
The problem may be that you are passing as an argument to os.path.getsize() just the folder name instead of the whole path to the folder
It may be that you have the file name as 'FordvFerrari1080p' rather than 'FordvFerrari1080p.mp4' (or whatever file type it may be)
This is my Python code:
from plugin import Plugin
import logging
import yaml
log = logging.getLogger('discord')
def get_bot_prefix():
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
prefixValue = yaml.safe_load(prefixReader)
prefixString = prefixValue['settings']['prefix']
return prefixString
prefix = get_bot_prefix()
However I got an error with the file accessing:
Traceback (most recent call last):
File "C:/Users/85251/Documents/Discord Bots/Python/HarTex/bot.py", line 20, in <module>
from plugins.help import Help
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 30, in <module>
prefix = get_bot_prefix()
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 22, in get_bot_prefix
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
FileNotFoundError: [Errno 2] No such file or directory: 'HarTex/hartexConfig.yaml'
How can I fix it? Or am I completely wrong with the directory?
The script should work if you are calling it from the parent directory of HarTex, maybe you are running it from a different working directory?
You could also try to open the file using the full path, as this is probably easy to check.
Error is very clear.You should use absolute path instead of relative path.
For an example home/Prakash/Desktop/test12/test.yaml
Your code definitely work,Once you will change path like this.
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.