How do you check if an excel file is open or not? If I can find out if it is closed, then I execute certain actions. If it is not closed, then I execute a different set of actions instead.
Asked
Active
Viewed 2,373 times
1 Answers
-2
This will not detect if the file is open by other processes!
try:
a = open('example.xlsx','w')
print 'file is closed'
execute_certain_actions(a)
a.close()
except IOError:
print 'file is open'
execute_different_set_of_actions()
This works because you cannot write to a file that is already open so the try statement will fail.
If you really want to make sure the file is not being used by any process, go check out this question.

Nathan
- 3,558
- 1
- 18
- 38
-
It may be OS dependent, but I think you can write to a file that's already open. – May 02 '18 at 22:12
-
@Evert at the company where I did my internship we always used this, never had issues – Nathan May 02 '18 at 22:18
-
@Nathan hmm can this try except code be within a while true loop and can if statements be within the Try code? I was looking more in line with something like 'if file is open' execute this else execute that. – superx May 02 '18 at 22:23
-
Open two Python interactive shells, open a file (in write-mode) in one, then open it in the other shell as well. No exceptions for me on MacOS. – May 02 '18 at 22:23
-
It's quite possible for another process to open the file after the `a.close()` before `execute_different_set_of_actions()` can execute, so this is not foolproof—even if you got away with it a lot somewhere your worked. – martineau May 02 '18 at 22:23
-
@superx I don't understand what you mean, but the answer (in my experience) is that you probably can make it work. What exactly are you planning on using it for (seeing if someone has the file open, or if another program is using it)? – Nathan May 02 '18 at 22:26
-
Yes you can be sure. If you can open it for writing **and keep it open**, then you can be certain that no other process also has it open for writing as well. – martineau May 02 '18 at 22:29
-
@Evert I'm not saying my answer is correct, but here https://stackoverflow.com/questions/6825994/check-if-a-file-is-open-in-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa somebody else used the same approach and got 29 upvotes – Nathan May 02 '18 at 22:29
-
Your code doesn't do what I am suggesting, so describing what it would do in relation to my previous comment as it's currently written isn't terribly relevant. – martineau May 02 '18 at 22:32
-
If the file can be opened without an exception, _then_ the open file could be passed to `execute_certain_actions()` as an argument. However if you close the file and then call `execute_certain_actions()` it's possible for some other process to open the file before that can happen. Got it now? – martineau May 02 '18 at 22:39
-
Yeah, that's what I meant...except now your statement that "This will not detect if the file is open by other processes!" isn't quite right. While that's the most likely reason for not being able to open it, strictly-speaking it's not the only possibility. – martineau May 02 '18 at 22:52
-
@martineau what I meant is that if another process (python script A) has opened it, it can still be opened. So I can still open it and write to it, but strictly speaking it is an open file. – Nathan May 02 '18 at 22:54
-
@Nathan what I mean is if there is anything that allows me to find out if a file is open or close by using an if else statement. Example: if excelfileA.isopen run this code. – superx May 02 '18 at 22:56
-
@Nathan One comment to your suggested answer mentions that this only works under special circumstances. Hence it's not bullet-proof. – May 02 '18 at 23:09
-
@Evert but the fact that it is still so widely excepted, to me, suggests it is probably the best we have right now – Nathan May 03 '18 at 05:04