4

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'

8
  • Are you sure your code is correct? shouldn't you use something like this: df2.to_excel(r'C:\BT\stack_test3.xlsx')? As I think your path is wrong and python thinks you are writing to c: as your back slash has not been escaped . Commented Sep 12, 2013 at 7:42
  • Does adding r prefix fix your problem? Commented Sep 12, 2013 at 8:37
  • No , unfortunally not..any new ideas? Commented Sep 12, 2013 at 8:50
  • You could check the access permissions, also could you try a different extension like .xls, .xlsx uses openpyxl , and .xls extension uses xlwt, would be interesting to see if this is a problem with both libraries. Commented Sep 12, 2013 at 9:00
  • When i use .xls it works!!! Commented Sep 12, 2013 at 9:05

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

11 Comments

@7stud it means to `c:` if you like the root or base of the disk drive, Vista and above may not allow applications to write to the root of your system disk unless it has sufficient privileges
But the docs say... Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the string; and if you try: s = 'A\B\C' print ord("\\") for ch in s: print ord(ch) you will see the backslashes are in the string. So the backslash should only create a problem if \B or \s is a recognized escape sequence, and neither is a recognized escape sequence. The op should use raw strings regardless to avoid any potential problems, or use forward slashes, but I don't see how this particular path is problematic.
Or even simpler, try: print len('A\B\C'). Compare to print len('C:\b\a')
@7stud I don't have the xls writer module but I did just try to_csv' without the r` prefix and got a [Errno 22] invalid mode ('w') or filename error, adding the r prefix fixes it, incidentally the expanded path without r prefix did not expand the middle slash so c:\\BT\stack_test3.xlsx would be displayed, if adding r prefix does not solve your problem then I will delete my answer
Okay, the MS docs say, if a backslash precedes a character that does not appear in the table, the compiler handles the undefined character as the character itself. For example, \c is treated as an c. So the string "C:\BT\stack_test3.xlsx" is interpreted by windows as C:BTstack_test3.xlsx
|
4

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.

Comments

1

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.

Comments

0

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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.