Hi,


I have a different question today:

how do I call the python interpreter AND tell it to open a python program from inside another program UNDER WINDOWS?

My program is running fine, and it even has a text mode without any graphical interface. Under Linux, you click on the 'text mode button', and it calls a console, which in turn calls python and tells it to open the program's non graphicl version.

Here is the code:

def textmode():
if os.name == "nt":
tkinter.messagebox.showwarning("Programmer error", "The programmer was not skilled enough to get this to work under Windows")
if os.name == "posix":
os.popen("xterm -geometry 160x40 -e python3.1 ./textversion.py")

as you can see, I'm calling xterm and telling it to open the text version. Now, how do I do it under windows? I have tried this:

c:\Program Files\Python3.1/python.exe ./textversion.py

but no luck...

Why not:

import os
os.startfile('textversion.py') ## program in same directory as this program

Oh... it was that simple, I feel dumb now...

Gotta read more about that startfile function, it's the second time that I saves me.

I assume this will also work on Linux right? If so this code is better than mine.

Thanks a lot, you are very helpful.

Oh, and congratulations, thi is your Nº 100 solved post :)

Unfortunately, the os.startfile function exists only in Windows. A multiplatform way is

import subprocess as sp
sp.call(["python", "textversion.py"])

Or, even better

import subprocess as sp
import sys
returncode = sp.call([sys.executable, "textversion.py"])
commented: Beauty FAQ answer (solution 2) +1

Oh... it was that simple, I feel dumb now...

Gotta read more about that startfile function, it's the second time that I saves me.

I assume this will also work on Linux right? If so this code is better than mine.

Thanks a lot, you are very helpful.

Oh, and congratulations, thi is your Nº 100 solved post :)

Hey, and yesterday post number 700 and Master Poster.

I gave you Windows specific answer as you asked it and had code in place for it.

Posting Doctor;) Gribouillis gives you the standard FAQ answer for generic. There is also some alternatives object oriented way for more complicated use cases. See his Code Snippet history.

I feel there should be some generic solution lurking maybe in MIME types handling or somewhere.... I would like to call default text editor to show file in generic way... No but that second answer of Gribouillis is a beuaty! Here comes reputation...

import os
os.system("python textversion.py")

Oh I see, I learned somethin new again, love this place.

Um, but all of the above solutions have a problem: they lock the main program, and if you close the text version, the main program is also closed.

You see, there is a button that executes these functions. The idea is to be able to have the text version and use whenever you feel like it. If you close the text version, the graphical one should stay there in case you wanna continue working with it.

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.