Permission error when pandas dataframe is write to xlsx file
Asked Answered
D

6

6

I get this error while i want to keep my dataframe in excel file which name pandas_simple.xlsx

Below is my error:

enter image description here

This is my code:

import pandas as pd
df = pd.DataFrame({'Car': [101, 20, 350, 20, 15, 320, 454]})
writer = pd.ExcelWriter('pandas_simple.xlsx')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
writer.close()

Anyone can share some idea to me here?

Domineer answered 3/8, 2017 at 8:41 Comment(4)
This may happen because the file is open in Excel or other program, or this folder doesn't have write permissions for you.Virgil
this excel file is new created when the code was runningDomineer
Does this work without writer = pd.ExcelWriter('pandas_simple.xlsx')? Try it with just df.to_excel(sheet_name='Sheet1') and drop at the writer extras.Cessation
@Virgil O. is correct. You either don't have permission to create a file in that location, or you have the previous version open and cannot overwrite it.Kironde
I
7

You try to write to a folder where you need administration rights. Change:

writer = pd.ExcelWriter("pandas_simple.xlsx")

to:

writer = pd.ExcelWriter("C:\\...\\pandas_simple.xlsx")

with the full path and you will not have a problem.

Innerve answered 3/8, 2017 at 9:5 Comment(0)
T
9

This error could also occur if you have a version of the same file (pandas_simple.xlsx in this case) already open on your desktop. In that case, python will not have permission to close and overwrite the same file as well. Closing the excel file and re-running the script should resolve the issue.

Tooth answered 15/1, 2019 at 23:7 Comment(0)
I
7

You try to write to a folder where you need administration rights. Change:

writer = pd.ExcelWriter("pandas_simple.xlsx")

to:

writer = pd.ExcelWriter("C:\\...\\pandas_simple.xlsx")

with the full path and you will not have a problem.

Innerve answered 3/8, 2017 at 9:5 Comment(0)
H
1

The documentation of pandas.DataFrame.to_excel says that the first argument can be a string that represents the file path. In your case i would drop all lines with writer and just try

df.to_excel('pandas_simple.xlsx')

That should write pandas_simple.xlsx to your current working directory. If that does not work try to provide the full path name (e.g. C:\\Users\\John\\pandas_simple.xlsx). Also make sure that you don't try to write to a directory which needs adminstration rights.

Haroldson answered 3/8, 2017 at 9:17 Comment(0)
B
1

Make sure you dont have the file open that you are trying to write to.

Bebel answered 21/12, 2021 at 16:49 Comment(0)
W
0

What if the path is correct?!!!

Try closing the xlsx file opened in Excel application and run the code again, it worked for me and same should happen with you.

I am attaching my code snippet for your reference


    import pandas as pd
    
    file='C:/Users/Aladahalli/Desktop/Book1.xlsx'
    xls = pd.ExcelFile(file)
    
    df = pd.read_excel(xls, sheet_name='Sheet1')
    
    #create a column by name Final and store concatinated columns
    df["Final"] = df["Name"] + "    " + df["Rank/Designation"] + "    " + df["PS"]
    print(df.head())
    
    # Create a Pandas Excel writer using XlsxWriter as the engine.
    writer = pd.ExcelWriter('C:/Users/Aladahalli/Desktop/Final.xlsx', engine='xlsxwriter')
    
    # Convert the dataframe to an XlsxWriter Excel object.
    df.to_excel(writer, sheet_name='Sheet1')
    
    # Close the Pandas Excel writer and output the Excel file.
    writer.save()
    writer.close()

Waechter answered 6/9, 2019 at 19:30 Comment(0)
M
0

I do have the same problem when I start the .py file via a double click (on a Windows 11 system).

The suggested reasons do not apply: The file I tried to write did not exist. Hence at was not open anywhere else. And yes, I do have write permission in the corresponding folder. Double click calls python3 to start the code in the .py file.

I noticed the following:

The problem is gone when I start the code in the command line of a terminal like so: python3 xyz.py

The problem is also gone when I create a batch file (.bat) that contains the line 'python3 xyz.py'.

Hence, my workaround is to create such a batch file so that I can start this batch file with a double click.

Problem solved for me. But it would be nice to know why a code that is started with a double click behaves that way.

Methodical answered 20/12, 2023 at 0:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.