I keep having the following error. you should know that file name is correct and this pandas method works in other py files, please help !!!!
the tablecouleurs is an excel table with no specific characters
import pandas as pd
colors=pd.read_excel('C:\Users\paul\tablecouleurs.xlsx', index_col=0, has_index_names=True)
and error:
runfile('C:/Users/paul/Documents/colors.py',
wdir='C:/Users/pauldufosse/Documents') Traceback (most recent call
last):
File "", line 1, in
runfile('C:/Users/paul/Documents/colors.py', wdir='C:/Users/pauldufosse/Documents')
File
"C:\Users\paul\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
line 685, in runfile
execfile(filename, namespace)
File
"C:\Users\paul\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/paul/Documents/colors.py", line 12, in
colors=pd.read_excel('C:\Users\pauldufosse\tablecouleurs.xlsx', index_col=0, has_index_names=True)
File
"C:\Users\paul\Anaconda\lib\site-packages\pandas\io\excel.py",
line 151, in read_excel
return ExcelFile(io, engine=engine).parse(sheetname=sheetname, **kwds)
File
"C:\Users\paul\Anaconda\lib\site-packages\pandas\io\excel.py",
line 188, in init
self.book = xlrd.open_workbook(io)
File
"C:\Users\paul\Anaconda\lib\site-packages\xlrd_init_.py",
line 394, in open_workbook
f = open(filename, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename:
'C:\Users\paul\tablecouleurs.xlsx'
Had the same problem. You can solve it by double escaping your path.
The error messages says:
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\Users\pauldufosse\tablecouleurs.xlsx'
Just do:
foo = pd.ExcelFile('C:\\Users\\pauldufosse\\tablecouleurs.xlsx')
This worked for me
open_workbook f = open(filename, 'rb')
If you check Python library you will see you have to use single quote instead of double quote.
Related
I am encountering a problem when running with openpyxl the code below
import openpyxl
import os
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
sheet["A1"].value
sheet["A1"].value == None
sheet["A1"].value = 42
sheet["A3"].value = 'Hello'
os.chdir("/Users/mac/Desktop")
wb.save('exceeeel.xlsx')
The error is
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 312, in load_wo
rkbook
reader = ExcelReader(filename, read_only, keep_vba,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 124, in __init_
_
self.archive = _validate_archive(fn)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 96, in _validat
e_archive
What am I doing wrong? I am using the current version of the openpyxl library.
I can't provide a confident answer because the question only includes a partial traceback. That being said, it looks like the traceback one would get for a FileNotFoundError:
C:\Python37\python.exe C:/Users/user/PycharmProjects/scratch/scratch2.py
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/scratch/scratch2.py", line 3, in <module>
wb = openpyxl.load_workbook('example.xlsx')
File "C:\Python37\lib\site-packages\openpyxl\reader\excel.py", line 313, in load_workbook
data_only, keep_links)
File "C:\Python37\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
self.archive = _validate_archive(fn)
File "C:\Python37\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
archive = ZipFile(filename, 'r')
File "C:\Python37\lib\zipfile.py", line 1207, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'example.xlsx'
Process finished with exit code 1
This error will be raised when the path your provided to the file you want to load with openpyxl.load_workbook does not contain the specified file. Since the only argument you provided in your call of that function is 'example.xlsx' that probably means there is no file in the folder you are running this script from.
If this 'example.xlsx' file is in a different folder then you'll want to either specify the relative path to that file as your argument or move the file into the same folder as your script.
If this isn't what's going on then you'll need to provide the full traceback that you are seeing on your end in order to get a better answer.
i have a txt file and if i try to open it python says:
runfile('/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python/untitled0.py', wdir='/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python')
Traceback (most recent call last):
File "<ipython-input-30-b4bdfdd17ca2>", line 1, in <module>
runfile('/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python/untitled0.py', wdir='/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python')
File "/Users/costanzanaldi/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Users/costanzanaldi/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python/untitled0.py", line 13, in <module>
in_file = open("POLO_SCIENTIFICO_(LAMMA).txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'POLO_SCIENTIFICO_(LAMMA).txt'
1)the file EXISTS!
2)the path is correct! It is in the desktop!
You need to change your directory to your desktop in order to access the file. You'd do that using the os module, like this:
import os
os.chdir("/path/to/Desktop")
Presumably this means the file is not on the desktop. (I assume you changed Ethan's "/path/to/Desktop" to the correct path in your system, which is "/Users/costanzanaldi/Desktop"?). Wherever it is, you need to give open() the full path to POLO_SCIENTIFICO_(LAMMA).txt unless you have chdir'd to the correct directory. I assume you have got the letter-case correct in POLO_SCIENTIFICO_(LAMMA).txt :-)
I am using Pandas on Mac, to read and write a CSV file, and the weird thing is when using full path, it has error and when using just a file name, it works. I post my code which works and which not works in my comments below, and also detailed error messages. Anyone have any good ideas?
sourceDf = pd.read_csv(path_to_csv)
sourceDf['nameFull'] = sourceDf['nameFirst'] + ' ' + sourceDf['nameLast']
sourceDf.to_csv('newMaster.csv') # working
sourceDf.to_csv('~/Downloads/newMaster.csv') # not working
Traceback (most recent call last):
File "/Users/foo/PycharmProjects/DataWranglingTest/CSVTest1.py", line 36, in <module>
add_full_name(path_to_csv, path_to_new_csv)
File "/Users/foo/PycharmProjects/DataWranglingTest/CSVTest1.py", line 28, in add_full_name
sourceDf.to_csv('~/Downloads/newMaster.csv')
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 1189, in to_csv
formatter.save()
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/format.py", line 1442, in save
encoding=self.encoding)
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/common.py", line 2831, in _get_handle
f = open(path, mode)
IOError: [Errno 2] No such file or directory: '~/Downloads/newMaster.csv'
Tried to use prefix r, but not working,
path_to_csv = r'~/Downloads/Master.csv'
path_to_new_csv = r'~/Downloads/Master_new.csv'
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 1189, in to_csv
formatter.save()
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/format.py", line 1442, in save
encoding=self.encoding)
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/common.py", line 2831, in _get_handle
f = open(path, mode)
IOError: [Errno 2] No such file or directory: '~/Downloads/Master_new.csv'
thanks in advance,
Lin
Try using os.path.join().
import os
(...)
output_filename = 'newMaster.csv'
output_path = os.path.join('Downloads', output_filename)
(...)
sourceDf.to_csv(output_path)
Use the same methodology to point pandas.read_csv() in the right direction.
You didn't specify python version.
On 3.4 you can use pathlib, otherwise use os.path.join() or quoting:
sourceDf.to_csv(r'~/Downloads/newMaster.csv')
Notice the r.
The problem is that /n is newline, which is not allowed in a path.
Traceback (most recent call last):
File "C:/my_python/read1.py", line 3, in <module>
book = open_workbook('Book1.xls',on_demand=True)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 449, in open_workbook
ragged_rows=ragged_rows,
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 941, in biff2_8_load
f = open(filename, open_mode)
IOError: [Errno 2] No such file or directory: 'Book1.xls'
I am getting this error message in python while trying to open an excel sheet.....How can i solve this ?
Either make sure Book1.xls is in "C:\my_python\", which is also where your script is, or use the full path to where Bool1.xls is located. For example:
book = open_workbook('C:\\Users\\Jeeva\\Documents\\Book1.xls',on_demand=True)
I have a python script that analyses a file tree and records its findings in an xlsx.
Analysis is going fine, but when I try to record my results, I get an error:
Traceback (most recent call last):
File ".\call_validation.py", line 103, in <module>
wb.save(wb_name)
File "C:\Python\lib\site-packages\openpyxl\workbook\workbook.py", line 298, i
save_workbook(self, filename)
File "C:\Python\lib\site-packages\openpyxl\writer\excel.py", line 196, in sav
writer.save(filename, as_template=as_template)
File "C:\Python\lib\site-packages\openpyxl\writer\excel.py", line 178, in sav
archive = ZipFile(filename, 'w', ZIP_DEFLATED)
File "C:\Python\lib\zipfile.py", line 923, in __init__
self.fp = io.open(file, modeDict[mode])
OSError: [Errno 22] Invalid argument: 'move_generated-2015-05-07 10:08:26.xlsx'
I am generating my filename using datetime.datetime.now() like so:
save_time = str(datetime.datetime.now()).split(".")[0]
wb_name = "move_generated-" + save_time + ".xlsx"
wb.save(wb_name)
I don't believe the filename is too long, its only in C:\code\call_flow and I've tried stripping all the non-alphanumeric characters out of the name. Any ideas?
EDIT: Solution ended up being that I had failed to strip the colons from the time. As #nivix zixer said I fixed it by replacing
save_time = str(datetime.datetime.now()).split(".")[0]
with
save_time = str(datetime.datetime.now()).split(".")[0].replace(':', '_')
Perhaps the problem is you have a space in the filename?
Replace str(datetime.datetime.now()).split(".")[0] with this: str(datetime.datetime.now()).split(".")[0].replace(' ', '_').
Glad I could help Will!