On Mon, Jun 21, 2010 at 7:18 AM, shanti bhushan <ershantibhus...@gmail.com>wrote:
> On Jun 21, 2:15 pm, Tim Golden <m...@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" > Your problem seems to be with *stdout* redirect to the same file: ">YourOutput1.txt". Windows is not like Unix like systems! You can try, instead of redirecting to the same file, redirect each to a separate file and use the following form: <code> proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:\372\pythonweb\mongoose-2.8.exe -root D:\New1\ 1>YourOutput.txt 2>&1"') </code> I hope it helps!
-- http://mail.python.org/mailman/listinfo/python-list