Re: [Tutor] Delete file before function ends
Adrian Greyling wrote: Not sure if this is possible, but I'll ask anyway. Below is a code snippet that creates my problem... What I'd like to do, is create a plain text file, use the associated program to open said textfile, (using os.startfile) and after the associated program has what it needs to open the file and then of course, has the current focus, I'd like to delete the text file in the background, so to speak. (Please assume that the program doesn't lock 'mytextfile.xyz' when it opens it.) What happens with the code snippet below, is that it doesn't really start the second program until the function is finished. I tried using time.sleep() in between the os.startfile() and os.remove(), but it just delays opening 'mytextfile.xyz' and actually deletes the file before my second program can open it up. Any way around this?? path = c:\MyFolder\mytextfile.xyz #bunch of stuff here to create 'mytextfile.xyz os.startfile(path) os.remove(path) Strange. I would have expected the opposite effect: the os.startfile runs (notepad, or whatever) but doesn't return control until it's complete. In any case, try the following snippet. The sleep is needed because the app probably won't launch in time to get there before the remove does. code import os import subprocess import time FILENAME = os.path.abspath (temp.txt) f = open (FILENAME, w) f.write (blah blah) f.close () subprocess.Popen ([FILENAME], shell=True) time.sleep (1.0) os.remove (FILENAME) /code TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Delete file before function ends
Adrian Greyling wrote: path = c:\MyFolder\mytextfile.xyz BTW, I hope you're using raw strings in your real code, or else you're going to come a cropper one day when a filename begins with t. TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Delete file before function ends
Adrian Greyling [EMAIL PROTECTED] wrote that creates my problem... What I'd like to do, is create a plain text file, use the associated program to open said textfile, (using os.startfile) and after the associated program has what it needs to open the file and then of course, has the current focus, I'd like to delete the text file Thats potentially going to cause the associated program to crash but assuming you know what you are doing there... What happens with the code snippet below, is that it doesn't start the second program until the function is finished. Correct, that's what you asked it to do :-) time.sleep() in between the os.startfile() and os.remove(), but it just delays opening 'mytextfile.xyz' and actually deletes the file before my second program can open it up. Really? That shouldn't happen! path = c:\MyFolder\mytextfile.xyz You probably want to either use forward slashes or put an r in front of the quotes, otherwise Python will treat the \ as an escape character... #bunch of stuff here to create 'mytextfile.xyz os.startfile(path) os.remove(path) If you want the remove to run in parallel with the startfile you probably need to use threads to start the application in one thread and then pause and then delete the file in the other thread. Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Delete file before function ends
Thanks for the input folks. Sadly, Tim's suggestion yields the same results as I was getting previously. My second program very graciously tells me that the file I'm trying to open doesn't exist. Like the code snippet I posted, the timer.sleep(x) line just waits the 'x' seconds until opening 'mytextfile.xyz, instead of opening it, and then waiting 'x' seconds to delete the file. Sorry about naming the path to my file so poorly!! I'm a little more careful in my programs! I'm a newbie and I was more concerned about an understandable question! As a newbie, Alan, I was kinda scared you'd say that threads were the answer here! (It sounds like someone is going to get sucked into a worm hole or something...) Looks like the next class in my Python education is going to be Threads 101... Thanks for all the input, I might even be learning something! Warmest regards, Adrian On Mon, Oct 6, 2008 at 1:31 PM, Alan Gauld [EMAIL PROTECTED]wrote: Adrian Greyling [EMAIL PROTECTED] wrote that creates my problem... What I'd like to do, is create a plain text file, use the associated program to open said textfile, (using os.startfile) and after the associated program has what it needs to open the file and then of course, has the current focus, I'd like to delete the text file Thats potentially going to cause the associated program to crash but assuming you know what you are doing there... What happens with the code snippet below, is that it doesn't start the second program until the function is finished. Correct, that's what you asked it to do :-) time.sleep() in between the os.startfile() and os.remove(), but it just delays opening 'mytextfile.xyz' and actually deletes the file before my second program can open it up. Really? That shouldn't happen! path = c:\MyFolder\mytextfile.xyz You probably want to either use forward slashes or put an r in front of the quotes, otherwise Python will treat the \ as an escape character... #bunch of stuff here to create 'mytextfile.xyz os.startfile(path) os.remove(path) If you want the remove to run in parallel with the startfile you probably need to use threads to start the application in one thread and then pause and then delete the file in the other thread. Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Delete file before function ends
On Mon, Oct 06, 2008 at 02:15:06PM -0400, Adrian Greyling wrote: As a newbie, Alan, I was kinda scared you'd say that threads were the answer here! (It sounds like someone is going to get sucked into a worm hole or something...) Looks like the next class in my Python education is going to be Threads 101... Threads are one approach, but if you aren't ready to jump into them, that's not the only way to solve your problem. However, please understand that your application is trying to do something that's a bit more advanced than the newbie level. Any time you need to synchronize the operation of multiple programs, there are complications and platform dependencies involved that may not be as simple as you first think. One natural, but naive, approach is to simply drop in a delay loop or call to time.sleep() to have the parent program wait long enough for the child program to have done what it needs to do. The problem is you are only guessing how long is long enough, and will probably be wrong enough of the time to be a problem. You need to work out some way to have the parent program wait for a definite signal that the child is finished before moving forward. That could be to use a subprocess invocation method which guarantees the child will complete before it returns. Or it could be that you watch for an artifact created by the child, or wait for the child process (or thread) to exit, or any of several other things. Browse through the subprocess module for some hints of things you can try. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Delete file before function ends
What OS are you using, Adrian? On WinXP, this worked fine: import os def files(): os.startfile('myfile.txt') os.remove('myfile.txt') -Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Delete file before function ends
I'll take a peek at the subprocess docs Steve, and see if I can learn something there... Thanks for the suggestion! I'm using WinXP as well Wayne.. Not too sure why yours works, and mine doesn't.. I'll revisit what I'm doing and also check into Steve's suggestion. Adrian On Mon, Oct 6, 2008 at 2:30 PM, W W [EMAIL PROTECTED] wrote: What OS are you using, Adrian? On WinXP, this worked fine: import os def files(): os.startfile('myfile.txt') os.remove('myfile.txt') -Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor