how can I detect if a file is open by windows app in WSL and either overwrite or rename?

21 Views Asked by At

I have a python script that dumps data into an Excel file. My most common user error is that I have the Excel file open, and then I run the script again which attempts to write the same file. This fails silently within the Python program and does NOT update the excel file with the new data, leaving me to facepalm myself, remember that I have to explicitly close the excel file, then I have to re-run the script which takes quite a while. This makes me feel dumb and I would like to feel less dumb. So, some questions...

is there a way in this environment (python 3.10 under WSL on a windows 11 machine) to identify if the given potential target file is already open? If so, is there a way to force-overwrite it anyway (I think not....)

Alternatively, if I can identify that the file is open I could throw some warning or "press to continue after closing file" or whatever... or i could write some logic that just chooses a derivitive filename from what's given and writes to that with a warning...

but I think the main part is figuring out whether the file is open by some other process and whether or not that behavior is always the same (is there a way to open a file that does NOT block other writes?)

0

There are 0 best solutions below