How to get permission for file editing in python(pandas)
Question:
I am trying to modify existing excel file on Windows using Python via Pandas, but the program gives me an error.
This is a sample of my simple program:
df_read = pd.read_excel("C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx")
df = pd.DataFrame({"Время":[1], "Сумма счета":[1], "Столы":[1], "Заказ":[1]})
df = df.append({"Время":1, "Сумма счета":1, "Столы":1, "Заказ":1}, ignore_index = True)
path = "C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx"
assert os.path.isfile(path)
writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
writer.save()
Here is the error:
Traceback (most recent call last):
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 320, in close
self._store_workbook()
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 638, in _store_workbook
raise e
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 635, in _store_workbook
xlsx_file = ZipFile(self.filename, "w", compression=ZIP_DEFLATED,
File "C:Users77888AppDataLocalProgramsPythonPython39libzipfile.py", line 1239, in init
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: ‘C:Users77888DesktopHKR_ОТЧЕТЫНоябрь 20202020-11-03.xlsx’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Users77888DesktopHKR_ОТЧЕТЫcheck.pu", line 12, in
writer.save()
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagespandasioexcel_xlsxwriter.py", line 193, in save
return self.book.close()
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 322, in close
raise FileCreateError(e)
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: ‘C:Users77888DesktopHKR_ОТЧЕТЫНоябрь 20202020-11-03.xlsx’
[Finished in 1.531s]
Answers:
right click and run code with administrator permissions.
The most common cause of this issue on Windows is that the xlsx file being created is already open in Excel. For example:
# A simple pandas/xlsxwriter program.
C:jmcnamara>type pandas_simple.py
import pandas as pd
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
# Run the program.
C:jmcnamara>python pandas_simple.py
# Open the file in Excel.
C:jmcnamara>pandas_simple.xlsx
# Try to rerun the program while the file is open.
C:jmcnamara>python pandas_simple.py
Traceback (most recent call last):
File "C:pythonxlsxwriterworkbook.py", line 320, in close
File "C:pythonxlsxwriterworkbook.py", line 638, in _store_workbook
File "C:pythonxlsxwriterworkbook.py", line 636, in _store_workbook
File "C:pythonzipfile.py", line 1204, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas_simple.py", line 8, in <module>
writer.save()
File "C:pythonsite-packagespandasioexcel.py", line 1732, in save
return self.book.close()
File "C:pythonxlsxwriterworkbook.py", line 322, in close
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
As you can see the error is similar to yours.
This warning can also happen if you don’t have write permissions for the output directory. That should be easy to check by trying to create a file in the target directory.
Through changing the privileges by running the programs you want use as "Run as administrator" for example I was using Excel in Pycharm so I changed to administrator privileges for both excel and Pycharm and got no error.
I am trying to modify existing excel file on Windows using Python via Pandas, but the program gives me an error.
This is a sample of my simple program:
df_read = pd.read_excel("C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx")
df = pd.DataFrame({"Время":[1], "Сумма счета":[1], "Столы":[1], "Заказ":[1]})
df = df.append({"Время":1, "Сумма счета":1, "Столы":1, "Заказ":1}, ignore_index = True)
path = "C:\Users\77888\Desktop\HKR_ОТЧЕТЫ\Ноябрь 2020\2020-11-03.xlsx"
assert os.path.isfile(path)
writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
writer.save()
Here is the error:
Traceback (most recent call last):
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 320, in close
self._store_workbook()
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 638, in _store_workbook
raise e
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 635, in _store_workbook
xlsx_file = ZipFile(self.filename, "w", compression=ZIP_DEFLATED,
File "C:Users77888AppDataLocalProgramsPythonPython39libzipfile.py", line 1239, in init
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: ‘C:Users77888DesktopHKR_ОТЧЕТЫНоябрь 20202020-11-03.xlsx’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Users77888DesktopHKR_ОТЧЕТЫcheck.pu", line 12, in
writer.save()
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagespandasioexcel_xlsxwriter.py", line 193, in save
return self.book.close()
File "C:Users77888AppDataLocalProgramsPythonPython39libsite-packagesxlsxwriterworkbook.py", line 322, in close
raise FileCreateError(e)
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: ‘C:Users77888DesktopHKR_ОТЧЕТЫНоябрь 20202020-11-03.xlsx’
[Finished in 1.531s]
right click and run code with administrator permissions.
The most common cause of this issue on Windows is that the xlsx file being created is already open in Excel. For example:
# A simple pandas/xlsxwriter program.
C:jmcnamara>type pandas_simple.py
import pandas as pd
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
# Run the program.
C:jmcnamara>python pandas_simple.py
# Open the file in Excel.
C:jmcnamara>pandas_simple.xlsx
# Try to rerun the program while the file is open.
C:jmcnamara>python pandas_simple.py
Traceback (most recent call last):
File "C:pythonxlsxwriterworkbook.py", line 320, in close
File "C:pythonxlsxwriterworkbook.py", line 638, in _store_workbook
File "C:pythonxlsxwriterworkbook.py", line 636, in _store_workbook
File "C:pythonzipfile.py", line 1204, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas_simple.py", line 8, in <module>
writer.save()
File "C:pythonsite-packagespandasioexcel.py", line 1732, in save
return self.book.close()
File "C:pythonxlsxwriterworkbook.py", line 322, in close
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: 'pandas_simple.xlsx'
As you can see the error is similar to yours.
This warning can also happen if you don’t have write permissions for the output directory. That should be easy to check by trying to create a file in the target directory.
Through changing the privileges by running the programs you want use as "Run as administrator" for example I was using Excel in Pycharm so I changed to administrator privileges for both excel and Pycharm and got no error.