File not being released
Question: Ok so I have to run some queries in access 07 then compact and repair it. I am
using python and win32com to do this. The code I'm currently using is this.

    
    
    import os;
    import win32com.client;
    DB1 = 'db1.mdb'
    DB2 = 'db1N.mdb'
    DB3 = 'db2.mdb'
    DBR = r'db1.mdb'
    access = win32com.client.Dispatch("Access.Application")
    access.OpenCurrentDatabase(DBR)
    DB = access.CurrentDb()
    access.DoCmd.OpenQuery("1")
    access.DoCmd.OpenQuery("2")
    access.DoCmd.OpenQuery("3")
    access.CloseCurrentDatabase()
    access.Application.Quit();
    os.system('copy "db1.mdb" "db2.mdb"')
    access = win32com.client.Dispatch("Access.Application")
    access.CompactRepair(DB3,DB2)
    access.Application.Quit();
    os.remove("db2.mdb")
    os.remove("db1.mdb")
    os.rename("db1N.mdb","db1.mdb")
    

The problem is that I get this error.

    
    
    WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'db1.mdb'
    

I dont know why I am getting this error seeing as I am quiting access which
should close the file. Any Idea how to fix this would be greatly appreciated.


Answer: Your code includes this line:

    
    
    DB = access.CurrentDb()
    

When that line executes, `CurrentDb()` references _db1.mdb_. Then you later
get _WindowsError_ at:

    
    
    os.remove("db1.mdb")
    

So I'm wondering if the variable DB still holds a reference to _db1.mdb_.
Perhaps you could first try `del DB` before `os.remove()`.

Or, since your code isn't actually using that DB variable, just get rid of it.

