process cannot access the file because it is being used by other process

Tim Golden mail at timgolden.me.uk
Mon Jun 21 05:15:47 EDT 2010


On 21/06/2010 09:23, shanti bhushan wrote:
> i am using below code ,it works fine on ordinary python 26 ,but when i
> use this script in my python testing tool it gives me message "process
> cannot access the file because it is being used by other process" for
> the second time invoking of mongoose server.
> Please help me in handling this exception.

Before I make any suggestions on the code, I might suggest that you
learn to wait a little. You sent three pretty much identical messages
within the space of three hours. Believe me: if people aren't helping,
it's not because they haven't seen your first message. Or the follow-up.
Or the one after that. It's because they don't know the answer, or
haven't the time to answer. Or aren't in the same timezone as you and
so haven't woken up yet!

> def invoke_server2():
>      file = open("mongoose-2.8.exe", "r")
>      try:
>          proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
> \372\pythonweb\mongoose-2.8.exe -root D:\New1\>YourOutput.txt"')
>      except OSError:
>          print "os error"
>          file.close()
>          sys.exc_clear()
>          os.remove("mongoose-2.8.exe")

OK. I'm not sure what you're achieving with the open ("mongoose...") line
and its corresponding close. In fact, I can't work out what the whole
exception block is achieving. I actually had to go and look up what
sys.exc_clear is doing -- and I don't think it's doing what you think
it's doing. You appear to be trapping an OS error, such as file-not-found
or access-denied, by trying to ignore the error and then deleting the 
server
itself!

Let's straighten some stuff out. First your Popen line could almost 
certainly
be simplified to this:

<code>
import subprocess

with open ("YourOutput.txt", "w") as outf:
   proc = subprocess.Popen (
     [r"D:\372\pythonweb\mongoose-2.8.exe", "-root", r"D:\New1"],
     stdout=outf
   )

</code>

and to kill the proc, you can just call proc.kill ()


Does that take you forward? Are you still seeing the "Cannot access file..."
errors?

TJG



More information about the Python-list mailing list