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

shanti bhushan ershantibhushan at gmail.com
Mon Jun 21 06:18:06 EDT 2010


On Jun 21, 2:15 pm, Tim Golden <m... at timgolden.me.uk> wrote:
> 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

i used below code

import subprocess
import time
def invoke_server1():
    proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
\372\pythonweb\mongoose-2.8.exe -root D:\New1\"')


invoke_server1()


time.sleep(10)
proc.kill()

this code only invokes the server but is not killing after 10 seconds.

my purpose is invoke server many times with different argument and
kill it.
but when ever  i use subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /
c
"taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"'
this gives me error "process cannot access the file because it is
being used by other process"



More information about the Python-list mailing list