Ok so, question about opening an application from python..is there a command that will force an app to open, while at the same time leaving the tkinter GUI open? The app I am trying to open is Snes9x.

import os
from tkinter import * 

try:
    import Tkinter as tk
    from urllib2 import urlopen

except ImportError:
    import tkinter as tk
    from urllib.request import urlopen

def videoGame():
    extensions =('.src', '.srm') 
    for data in os.listdir('/SNES/SNES Roms'):
        if data.endswith(extensions):
            os.path.normpath(r'/Users/SirPrinceKai/Applications/Snes9x')   
    return(videoGame)

Use the subprocess module

process = subprocess.Popen('Snes9x', shell = True)
commented: Thankyou Thankyou worked like a charm! :D +0

Actually, its showing that the function object has no attribute "Popen". Any thoughts?

Nevermind, forgot to import the module! a haha! Thanks G! :)

New code, added the above snipped, but now its telling me permission denied. Any way around this to force the GUI to open up my Snes9x emulator within my apps folder?

try:
    import Tkinter as tk
    from urllib2 import urlopen

except ImportError:
    import tkinter as tk
    from urllib.request import urlopen


def videoGame():
    '''This function opens up the Snes9x player in the Applications folder.'''
    print(videoGame.__doc__) 
    subprocess.call(['ls', '-l']) 
    extensions =('.src', '.srm') 
    for data in os.listdir('/SNES/SNES Roms'):
        if data.endswith(extensions):
            process = subprocess.Popen('/Applications/Snes9x.app', shell=True)

Permission denied means that the user who is running your python program doesn't have the permission to list the directory, or to run Snes9x, or that Snes9x is not an executable program, etc. It is a system issue, not a python issue.

Would that be the same case with the output "/Applications/Snes9x.app: is a directory?"

Would that be the same case with the output "/Applications/Snes9x.app: is a directory?"

It means that /Applications/Snes9x.app is a directory. Popen expects an executable program. Try the command in a terminal first. If it works in a terminal, it should work in python with Popen.

It works fine from terminal when I type in open '/Applications/Snes9x.app' but for some reason its listing it as a directory outside of the terminal. Weird.

But when I try and do it from terminal using the same line of code thats in my program (import subprocess, process = subprocess.Popen('/Applications/Snes9x.app', shell=False) it returns a "permission denied" error code, as seen below. Frustrating, to say the least. -_-

>>> import subprocess
>>> process = subprocess.Popen('/Applications/Snes9x.app', shell=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py", line 818, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py", line 1416, in _execute_child
    raise child_exception_type(errno_num, err_msg)
PermissionError: [Errno 13] Permission denied
>>> p

With shell = False, the first argument must be a list:

subprocess.Popen(['/Applications/Snes9x.app',], shell=False)

Changed the first argument to a list, still denied. Is it because I'm trying to launch Snes9x from my applications folder, and I need to do something to set my permissions correct before I run the program?

When I said a terminal, I did not mean a python shell, but an OS terminal or console. What happens when you type

/Applications/Snes9x.app

in a console ?

it spits back: is a directory, which is exactly what happens when I execute the code...it comes back as a directory. Funny thing is, if I type "open /Applications/Snes9x.app...it opens fine! :O

Then try subprocess.Popen("open /Applications/Snes9x.app", shell=True)

That did the trick! Next task..figure out the code to take a single game name as an input and open it with the proper emulator! :D

You're awesome!

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.