So my program is progressing quite nicely, and I'm semi-proud of myself for getting this far (I'm a newbie), but I had 2 questions. One question is that my program recognizes a .CFG extension...but never finds games with any other extension (.smc, .srm). I type it in EXACTLY as it is in the folder its in, and im sure that the function isnt written wrong because it returns True for the only game I had as a CFG format. Any ideas?

Also, how would one import photos and files and things to use in a python program (such as pictures for backgrounds, jpegs to be used inside the program, etc)?

Thanks! :)

import os, time, subprocess, shlex, logging, platform
from tkinter import * 


try:
    import Tkinter as tk
    from urllib2 import urlopen

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

def debug():
    if platform.platform().startswith('Windows'):
        logging_file = os.path.join(os.getenv('HOMEDRIVE'), os.getenv('HOMEPATH'), 'konsole_kollection.log')
    else:
        logging_file = os.path.join(os.getenv('HOME'), 'konsole_kollection.log')
    print('Logging To: ', logging_file)
    logging.basicConfig(
        level=logging.DEBUG,
        format ='%(asctime)s :  %(levelname)s : %(message)s',
        filename = logging_file,
        filemode = 'w',
    ) 

    logging.debug("Start")
    logging.info("Running")
    logging.warning("Warnings")
    logging.critical("P0 Errors")
    logging.error("P1 Errors") 

def videoGame():
    '''This function opens up the Snes9x player in the Applications folder.'''
    print(videoGame.__doc__)
    # export PYTHONPATH=/Users/xxxx/Desktop/Python3:$PYTHONPATH
    logging.debug('Initializing SNES Locator')
    extensions =('.src', '.srm', '.CFG') 
    subprocess.call(['ls', '/SNES/SNESRoms'])
    while True: 
        command_line = input('Enter the game you would like to search for: ')
        for data in os.listdir('/SNES/SNESRoms'):
            if command_line.endswith(extensions):
                arguments = shlex.split(command_line)
                print(arguments)
                master_process = subprocess.Popen(["open /Applications/Snes9x.app", command_line], shell=True, 
                executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False,
                cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
                logging.info('Running SNES Locator')
                return False
            else:
                print('Invalid input error, please enter a valid game format.')
                logging.error('Error Code 1: Invalid Input Error')
                break
    else:
        print(command_line) 

Dani AI

Generated

A few focused points that explain why the CFG file opens while the ROMs do not, plus a small, practical fix.

The core logic bug is that the loop iterates filenames into data but never uses data; instead it tests the typed string with endswith(extensions). That only validates the extension on the user input, not whether that filename exists in the folder. Also note the extensions tuple in the posted code contains '.CFG' (uppercase) while ROM files are lowercase — case handling matters on some systems. As and observed, the name from os.listdir() must actually be compared against the typed name; 's case-sensitivity hint is relevant too. Another practical pitfall: under Python 2 input() evaluates the input (use raw_input() there), and the subprocess invocation mixes a list with shell=True (either pass a single string with shell=True or pass a list and shell=False).

A simple, clear approach (Python 2/3 compatible ideas shown) — normalize the typed name, search the directory for a matching filename, build the full path, then launch the emulator with that full path:

# normalize and search
name = command_line.strip()
for fname in os.listdir(romdir):
    if fname.lower() == name.lower() and fname.lower().endswith(('.smc','.srm','.cfg')):
        path = os.path.join(romdir, fname)
        subprocess.call(['open', '-a', '/Applications/Snes9x.app', path])
        break
else:
    print('Not found or wrong extension')

Troubleshooting tips: print repr(command_line) to detect stray quotes/whitespace; use os.path.isfile() / os.path.exists() to confirm the constructed path; keep the allowed-extension tuple consistently lowercase and compare with lower(); avoid ls for machine logic — use os.listdir() for reliable results. This addresses the incorrect loop usage, case mismatches, and subprocess invocation that are preventing .smc/.srm files from being opened.

Recommended Answers

All 7 Replies

Your extensions may be case sensitive.

All the extensions in the snes folder are all lowercase, which is why they are lowercase in the code. Its just not recognizing anything but .CFG for some reason.

Do you want to search for a file name ending, "data" i.e. list of files, if not, state what you do want the program to do?? Now you do the same search [the result from input()] for each file name returned from os.listdir

    command_line = input('Enter the game you would like to search for: ')
    for data in os.listdir('/SNES/SNESRoms'):
        if command_line.endswith(extensions):

If the data endered ends with one of the 3 extensions, I would like it to be returned and the snes emulator to be opened. It works with one game with a .CFG function, but for the rest of the smc or srm files, it wont recognize and and kicks back the error code that I wrote (line 51). Basically, the basic gist of the program is to list the games, have the user input any game, and that game to be opened with Snes9x. :)

If the data endered ends with one of the 3 extensions, I would like it to be returned and the snes emulator to be opened.

Your logic dos not make sense to me.
If user input game.smc,should all files that has a smc extensions be found?
Explain your self better.

You never use data returned by os.listdir('/SNES/SNESRoms').
data return are all files in /SNES/SNESRoms,but if you never use it what's the point?

What i mean is this,here i use data to do something.

for data in os.listdir('/SNES/SNESRoms'):
    print data #all files in /SNES/SNESRoms that you never use
    if data.endswith(extensions):
        print data #all files that ends with extension definde before in extensions.

The subprocess.call lists every single rom inside of the SNES Roms folder, at that point the user is given the option to enter a full title of a game, and if that game is found, it should be open with SNES9x. For instance, if a game with the title of ZSNESW.CFG is found in the subprocess.call list, it should be popped open with the SNES9x player.

The only game that this works properly with IS the ZSNESW.GAME. Every other game that the user enters with a .smc or .srm extension is NEVER recognized by the player, and instead kicks back the error code and prompts the user to enter the game again. What it basically boils down to is, the only game that works is ZSNESW.CFG, but every single .smc or .srm file that is put in (and matches one of the names in the subprocess call) should be opened with SNES emulator.

So a small update, in case I forgot to mention, CFG is the configuration file for SNES, so it finds the config file...but not the rom files.

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.