Python Forum

Full Version: Changing file location
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I amend the following code so that "filename.xlsx" is created in say "D:\SomeFolder\" and not in the default project folder.

import xlsxwriter
workbook  = xlsxwriter.Workbook('filename.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(0, 0, 'Salaams')
workbook.close()
Change 'filename.xlsx' to 'D:/SomeFolder/filename.xlsx'
OK, that worked. Just a knock-on question. What if I want to save the file in the root D drive? I tried D:/filename.xlsx which gave an error?
(Sep-27-2022, 09:49 PM)azizrasul Wrote: [ -> ]OK, that worked. Just a knock-on question. What if I want to save the file in the root D drive? I tried D:/filename.xlsx which gave an error?

What was the error?
"D:\Power BI & Python\venv\Scripts\python.exe" "D:/Power BI & Python/Test.py" 
Traceback (most recent call last):
  File "D:\Power BI & Python\venv\lib\site-packages\xlsxwriter\workbook.py", line 324, in close
    self._store_workbook()
  File "D:\Power BI & Python\venv\lib\site-packages\xlsxwriter\workbook.py", line 659, in _store_workbook
    raise e
  File "D:\Power BI & Python\venv\lib\site-packages\xlsxwriter\workbook.py", line 656, in _store_workbook
    xlsx_file = ZipFile(self.filename, "w", compression=ZIP_DEFLATED,
  File "C:\Users\Aziz\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1249, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'D:/filename.xlsx'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Power BI & Python\Test.py", line 5, in <module>
    workbook.close()
  File "D:\Power BI & Python\venv\lib\site-packages\xlsxwriter\workbook.py", line 326, in close
    raise FileCreateError(e)
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied: 'D:/filename.xlsx'

Process finished with exit code 1
You're getting a permission denied. Looks like you don't have permission to save files directly into D:. But that's outside the scope of your program. You might have to look at filesystem permissions or ACLs. Can you save a file from notepad there either?
I tried to copy a text file and it came up with a dialog box with 3 buttons i.e. Continue, Skip and Cancel. The title of the dialog box was Destination Folder Accessed Denied. When I press Skip it copies the file over no problem. Similar thing happens when I then try to delete the file.