python pandas to excel error - python

I have a dataframe that I want to export to Excel. I'm new to python and pandas so I need some help on this simple task.
df2.to_excel('C:\BT\stack_test3.xlsx')
Error message:
IOError: [Errno 13] Permission denied: 'C:\BT\stack_test3.xlsx'

You path is incorrect, because you have not escaped the slashes it thinks you are trying to write to the root of c: drive use the following:
df2.to_excel(r'C:\BT\stack_test3.xlsx')
The r makes the path a raw string and means you do not need to escape the slashes
Edit
It seems that there is some error with openpyxl as using
df2.to_excel(r'C:\BT\stack_test3.xls')
works which uses xlwt, I don't know enough about those packages so it could be either a permissions problem with openpyxl which I have not been able to find anything about or a bug.

I had an identical problem. Turns out it's because I had left the Excel file open whilst I was trying to write to it. Apparently it doesn't like that. If you have it open try closing it.

To confirm...in case future readers stumble in this page...before complicating things make sure that the excel file you are trying to save is not already open or to be safe.
Just close all of excel and try save it again.
That should do it.

You should write to another drive like 'D:' because in Windows Vista or above that you have no permission to write to 'C:\' and you have no reason to earn the permission.

After closing all instances of excel and running
python code works.

Related

filenotfounderror: [errno 2] no such file or directory: .txt file

I am currently learning python and my instructor is telling me to open a text file using the open() meathod. I get the following error each time:
FileNotFoundError: [Errno 2] No such file or directory: 'movies.txt'
I have tried using online guides but all I could find was for .csv files, whereas I'm trying to open a text file. My complete code is as follows:
with open('movies.txt') as file_object:
contents = file_object.read()
print(contents.strip())
I've tried writing the file 'movies.txt' in VS code, and in my notepad, and then saving it to the same directory as the code but no use. I have also attempted to use a more exact file source, but still the same error. Sadly, google didn't have too much information for text files so I have to come here. Is there something I have to change in VS settings? I also tried my code in IDLE but not response either.
Thanks,
Stan
First of all you have to make sure that the file you are looking is in the same folder as your script as you are giving just the name and not the path.
Then the code to read a file misses a parameter:
with open('movies.txt', 'r') as file_object:
contents = file_object.read()
print(contents.strip())
r: read
w: write
a: append
The code is ok, the problem is finding the object as the error states. As you write it, it looks like "movies.txt" is in the same directory as the script. Are you sure they are in the same directory? Otherwise you will have to set the whole route
This is not about the directory your code is in. This is about the current working directory. These two directories may or may not be the same in some specific circumstance, but treating them as the same thing will lead to confusion in the future.
I don't know about VS Code (haven't worked with it), but all IDEs I've worked with have an option to set the current working for code you're running from the IDE.
I ended up using the run & debug function in VS, and it works. I dont understand what it debugged. All the information it gave me was my .vscode files? I guess its fine for now? Is it possible that the issue was caused by something else I have downloaded on my laptop?
Thanks,
Stan

Excessive indirect references in NAME formula

I am trying to read in an 'xls' files in python using pandas. My code basically is a one-liner:
import pandas as pd
df = pd.read_excel(str("/test/test_file.xls"))
This code works for the majority of the files, but there are cases when it fails with the error:
Excessive indirect references in NAME formula
What I tried so far:
Tried changing the stack limit(panic and warning) to as far as 10000 in the Pandas package itself, where the exception was occurring. A recursion limit was encountered, so raised it as far as 125000, which led to my Mac/Python reaching its limit so I am guessing not the right solution.
Used a memory-intensive EMR to see if it can read the file - nope.
Looked at the GitHub repo for XLRD here to raise a bug only to find out it's out of support.
Opened the file, saved it as an xlsx, used the same code to read it into a dataframe. Worked like a charm.
Tried using Spark Excel Library to read in a particular section of the data - this worked too but I need to use pandas.
Googled it only to find out the results would show me the XLRD code where the exception is defined. Not one person has reported it.
Tried using Python2 and Python3 with the latest and older versions of Pandas - no use.
I cannot share the file, but has anyone faced this issue before? Can someone help? All suggestions are welcome!
Try the following:
Open the xls file
Copy/paste all cells as values
Rerun your script
Hard to help further without having access to the file to explain exactly what is happening.
But chances are xlrd is trying to resolve the value of a formula and is exceeding the "STACK_PANIC_LEVEL". Without seeing the formula, very difficult to say more.
xlrd has a method of evaluate_name_formula(). When you try to open a .xls file with xlrd, it will raise an error (as you described) if your file has many user-defined formulas. To try to solve your problem, I think you can delete these user-defined formulas and keep the file free of these formulas. Or you can try to edit xlrd code, and prevent it from raising the Error, which seems much more difficult.

Loading file into Jupiter Notebook

Recently followed a short Python bootcamp and have been trying to work on it myself a bit more, but get stuck at the start. We need to upload the data, but for some reason I can't get it to work. In the example it was done like this:
Correct way apparently
So I figured it was just about the file path, so tried to copy that but don't get it to work
My wrong attempt
Any help would be thoroughly appreciated!
Did you try saving the notebook in the same directory where the CSV file exists and use a relative path to import the CSV?
data/titatic.csv
or
titatic.csv

Opening .DBF files as read-only with the dbf Python module

First of all, the dbf module is great. I've been using it with great success.
I'm trying to open a dbf file on a network share which is a read-only file system. When I try to open it like this, I get an error which says that the .dbf file is read-only.
thisTable = dbf.Table('/volumes/readOnlyVolume/thisFile.dbf')
thisTable.open()
Looking at the documentation, it looks like there's a way to open a table in read-only mode, but I can't figure it out. If you have a second, would you be able to help me out?
Thanks!
Kyle
Cool, thanks! :)
At this point, you need to specify the open mode when you call thisTable.open(), like this:
thisTable.open(mode='read-only')
or
thisTable.open(mode=dbf.READ_ONLY)
Oh, and here's the PyPI link to the module.
Assuming you're using this module, the magic incantation to open read only is:
dbf1 = Dbf()
dbf1.openFile('county.dbf', readOnly=1)
Hope that helped, if not, do add more detail to your question.

How to write to a read-only file in Python

Is there a way to write to a read-only file in Python? I am trying to write a script which helps me add debug statements at the start of every function in a given file. But the issue I have is that before I run the script, I have to manually remove the read-only flag on the file. is there anyway I can write to read-only files without manually having to remove them? Any suggestions will be deeply appreciated. Thanks.
If the user that runs the script doesn't have permissions to write in a file, you can't edit it. Basically, you need to have the w permission to edit a file. See Linux file permissions for more information.
If you want to get rid of it, you should make the file writable directly, or try to change its chmod with the os module, if you have enough permission to do this:
>>> os.chmod('path_to/file', 0755)
Try os.chmod() before opening the file.

Categories