Q:

Why Windows command Processor is exited after Opened the file in python?

I opened a file in python with the following code,
 s=subprocess.Popen(fileName,shell=True)
 s.wait()
 os.remove(fileName)

Whether the file is closed or not, it directly jumps to os.remove(), by that time file is not closed. So getting the following exception.
WindowsError: [Error 32] The process cannot access the file because it is being used by another process

The above problem is occuring in other machine, but in my machine until the file is closed code flow is not coming to os.remove()
How can I make subprocess.Popen() should wait until the file is closed.

A:

I tried in the Different way as follows,
def removeTempFile(tempFileName):
  f = None
  while f == None:
   try:
       f=open(tempFileName,'w')
       f.close()
   except Exception as e:
       print "already open"
       f=None
       time.sleep(2)

os.remove(tempFileName)

