You cannot tell if a file is used only if it has been used recently. The reason is that the moment the script returns the file can be closed by any program that previously used it. Writing scripts like this will only lead to poor performance.
A much better approach is to just do what the script planned if the file was not used and catch exceptions that result from a usage conflict. The end result will be a much simpler and more reliable program.
source
share