-1

I want to close an excel file after opening it. I searched this site but I cannot get a solution. Can anyone help?

import os

#TODO: close the file 'myexcel.xlsx' before writing

#writing data to 'myexcel.xlsx' here

a = os.startfile('myexcel.xlsx')#open the file for reading

Thank you very much.

Chan
  • 3,605
  • 9
  • 29
  • 60
  • 1
    What are you trying to do Depending on what you want to do, launching it as a subprocess and tracking it's process ID might be more interesting for you. – user1767754 Feb 08 '18 at 02:56

1 Answers1

1

This isn't possible using startfile(). According to the os.startfile documentation:

startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.

You might consider using the subprocess module. There are a load of options depending on what exactly you're trying to do at this question: How to terminate a python subprocess launched with shell=True

sam
  • 366
  • 1
  • 11
  • Can we kill the process in Windows if 'myexcel.xlsx' is opened? – Chan Feb 08 '18 at 03:10
  • Using `startfile()`? I don't believe so -- not without using `taskkill`. Maybe this would be of help there? https://stackoverflow.com/questions/6278847/is-it-possible-to-kill-a-process-on-windows-from-within-python – sam Feb 08 '18 at 03:16