[Tutor] Delete file before function ends

Alan Gauld alan.gauld at btinternet.com
Mon Oct 6 19:31:54 CEST 2008

"Adrian Greyling" <adrian.greyling at gmail.com> 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.


More information about the Tutor mailing list