I'm working on a simple update function to add to an existing program. It does a stat check of the source file and then copies the new file if necessary. This is easily accomplished with .py files. I've realized that I need a separate update app to delete a currently running 'exe'. My question is how do you find the path of the file that started the update file. ie, if I write os.startfile(xxx) when xxx starts is there a way to find the path of the original file?
I've been looking through the sys module but havent found anything.

I don't believe that there is... startfile simply launches the program as if it had been executed from the command prompt/shell... It doesn't return anything as far as I can tell, so my answer would be no.

But perhaps there is a way.

For anyone searching this looking for an answer, I made a simple workaround vbs script. The app builds it, executes it and then closes itself. The vbs has a delay built into it so file access doesnt overlap. Credit for the vbs code goes to http://www.autohotkey.com/forum/topic1572.html

in the following code the variable ufile is the filename of the update file

def build_vbs():
code = '''Wscript.Sleep 1000
Dim fso, MyFile, objFileCopy
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.GetFile("'''+progpath+'''")
MyFile.Delete

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileCopy = objFSO.GetFile("'''+ufile+'''")
objFileCopy.Copy ("'''+progpath+'''")


'Delete the currently executing script
Dim objFSO 'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile WScript.ScriptFullName
Set objFSO = Nothing'''
filename = progdir+"\\update.vbs"
File = open(filename, "w")
File.write(code)
File.close()
os.startfile(filename)
window.Close(True)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.