2

I am writing to a file from python, if the file is open in some process, python throws error. To make it clear, I am writing to a excel file, I want it closed if already open.

This is below code I use to write to file -

writer = pd.ExcelWriter('file_Output.xlsx', engine='xlsxwriter')
file.to_excel(writer,index=False, sheet_name='Sheet1')

Which throws below error if the file - file_Output.xlsx is already open in excel.

Traceback (most recent call last):
  File "pythonclose.py", line 311, in <module>
    writer.save()
  File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\excel.py", line 1952, in save
    return self.book.close()
  File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\site-packages\xlsxwriter\workbook.py", line 306, in close
    self._store_workbook()
  File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\site-packages\xlsxwriter\workbook.py", line 655, in _store_workbook
    allowZip64=self.allow_zip64)
  File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\zipfile.py", line 1082, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'file_Output.xlsx'
amanb
  • 5,276
  • 3
  • 19
  • 38
Abhinav Kumar
  • 177
  • 2
  • 5
  • 22
  • This is a filesystem level error. It looks like the user running the python script does not have the correct permissions to write to that file. Are you sure it's because the file is open? Normally that would not cause a permission denied error. – ty. Apr 18 '19 at 18:58
  • @ty. yes I am sure this error is there just because I have this Excel file open in MS Excel, works very fine If I close this MS Excel file, I am able to write to the given file then. So I want to close the MS Excel process with that file open. – Abhinav Kumar Apr 18 '19 at 19:03
  • @ty, the 'Permission denied' error __does__ appear when the file is open. – amanb Apr 18 '19 at 19:04
  • Are you only dealing with Excel files? – amanb Apr 18 '19 at 19:10
  • @amanb for now, YES, as most of the times I am writing Excel only, but files maybe any , maybe word file or a jpg image. – Abhinav Kumar Apr 18 '19 at 19:11
  • ok, and it doesn't matter to you if there is unsaved information on the files while you are closing them? – amanb Apr 18 '19 at 19:12
  • @amanb : i have also encountered similar error with pdf files also. – Rahul Agarwal Apr 18 '19 at 19:13
  • @amanb no I simply want file closed, I want the file closed and data written. Unsaved data is not a worry here. – Abhinav Kumar Apr 18 '19 at 19:14
  • I'd suggest closing Excel altogether instead of just the one file, if that is possible. This is because Python does not have any control over the file if Python itself did not open it. The file handle is with another program, in that case. If you are okay with closing Excel, then there are solutions available to kill Excel processes. – amanb Apr 18 '19 at 19:41
  • @amanb while I don't care about one particular file i want to close, closing the whole excel process may close many other files as well, now I cannot do this as I am sure about one file only where saved/ unsaved doesn't matters. other files may be important. So I want to close 1 particular specified file. – Abhinav Kumar Apr 18 '19 at 19:44

2 Answers2

1

If a file is open by an external program(such as MS Excel in this case), the file handle or control of the file is with the program. Python has no control over the file and cannot close/edit it. However, if you have the Process ID for the file, it is possible to kill the process by different means(for eg. using taskkill in Windows or even with Python).

amanb
  • 5,276
  • 3
  • 19
  • 38
  • Taskkill will kill the process, There are 5 Excel instances open it will kill all, I wish to close just one. I hope I am not wrong, correct me. – Abhinav Kumar Apr 19 '19 at 07:49
  • 1
    There may be multiple instances of Excel open but the file itself will have a unique Process ID. You can kill only that process if you have its ID. You can use [Process Explorer](https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer) from Sysinternals to scan through processes. – amanb Apr 19 '19 at 07:51
  • yes closing it by Sysinternals is ok but I want to be limited to python only libraries, anyways the previous link is helpful I will try with that. – Abhinav Kumar Apr 19 '19 at 07:53
  • 1
    Process Explorer can give you information(such as ID) for a process and can also help you kill it. But, if you want to kill the process purely with Python, you can use the ID and do it as mentioned in links in my answer. – amanb Apr 19 '19 at 07:55
0

I have this situation too. I decided to append a random digit to the filename when I create the file so it won't collide with the open version. Then later I just clean up the previous versions. When I or a user go to open the file using Excel, just grab the newest one. This could also be done with a date/time string in the filename which would appear more logical to users seeing the files.

BrownInTown
  • 43
  • 1
  • 7
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34411796) – Radinator May 22 '23 at 12:08
  • You're right--but it is a way to avoid the issue raised in the question--a workaround perhaps? – BrownInTown May 22 '23 at 22:04