Right. Basically I'm new to Python ( Go on call me a noob) and I'm trying to make a program that will open certain programs when you give the right password. But i just can't seem to get it to open the right files. So far, I am only trying it out on Mozilla Firefox and Nmap - Zenmap GUI. here's the code if you want to see it:
------------------------------------------------------------------------------
def get_password():
for k in range(1):

encoded = "sqrMHtl8"
password = raw_input("Enter your password: ")
if password == encoded.encode('rot13'):
return True
return False

def start():
if get_password():
print "Success!"
def menu():
print "Welcome to Program Selector"
print "Your Options are: "
print " "
print "1. Mozilla Firefox"
print "2. Nmap - Zenmap GUI"
print "3. Exit this program"
print " "
return input ("Choose your program: ")
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
import os
os.system("cd \..\Program Files\Mozilla Firefox\ Start firefox.exe")
loop = 0
elif choice == 2:
import os
os.system("cd \..\Program Files\Nmap\zenmap\ START zenmap.exe")
loop = 0
elif choice == 3:
loop = 0
else:
print "Wrong password!"


if __name__ == "__main__":
start()

os.system("cd \..\Program Files\Mozilla Firefox\ Start firefox.exe")

"cd" = change directiory. You want to execute the program which you should be able to do by calling it from os.system()
os.system("\..\Program Files\Mozilla Firefox\ Start firefox.exe")
Check this directory to make sure you want to call "Start firefox.exe" instead of "firefox.exe" or "/Start/firefox.exe". I'm not familiar with MS windows but the file has to exist in order to call it. os.system is the same as clicking on the file, so you want whatever name you would click on.

But you should get used to using subprocess instead of os.system as it is the preferred way.
subprocess.call(["\..\Program Files\Mozilla Firefox\ Start firefox.exe"], shell=True)
You can find info about os.system and subprocess here http://www.doughellmann.com/PyMOTW/modindex.html

Thanks for the help... I'll try that :D

In the future, please use code tags when posting code in this forum, it makes your code actually readable instead of a wall of unformatted, garbled text.

Use code tags like this:
[code=python] # Code inside here

[/code]

oh ok my bad but i still cant get it to work

oh ok my bad but i still cant get it to work

Well show us your changed code so we can have a look at it then.

Right this is my changed code:

def get_password():
    for k in range(1):
        
        encoded = "#password"
        password = raw_input("Enter your password: ")
        if password == encoded.encode('rot13'):
            return True
    return False

def start():
    if get_password():
        print "Success!"
        def menu():
            print "Welcome to Program Selector"
            print "Your Options are: "
            print " "
            print "1. Mozilla Firefox"
            print "2. Exit this program"
            print " "
            return input ("Choose your program: ")
        loop = 1
        choice = 0
        while loop == 1:
            choice = menu()
            if choice == 1:
                import subprocess
                subprocess.call(["\..\Program Files\Mozilla Firefox\firefox.exe"])
                loop = 0
            elif choice == 2:
                loop = 0
    else:
        print "Wrong password!"
        

if __name__ == "__main__":
    start()

And that about sums it up

subprocess.call(["\..\Program Files\Mozilla Firefox\firefox.exe"])

Have you verified that relative to where this script is running, \..\Program Files\Mozilla Firefox\firefox.exe is the correct path?

I think that I've run into problems using path expansion principals within Python as it's something that I (as a mostly command-line user) take for granted. Your best bet is to probably just use the absolute path C:\\Program Files\\Mozilla Firefox\\firefox.exe Ahah! Actually, as I typed that path out I realize what may be your mistake. In Python the \ is the escape character, so the interpreter automatically assumes any single backslashes designate an escape character (such as tab, newline, null, etc.). As a result you need to use \\ to indicate you actually want a backslash and not a special character. Here's an example to illustrate:

>>> print "\..\Program Files\Mozilla Firefox\firefox.exe"
\..\Program Files\Mozilla Firefoxirefox.exe
>>> print "\\..\\Program Files\\Mozilla Firefox\\firefox.exe"
\..\Program Files\Mozilla Firefox\firefox.exe
>>>

Hopefully that simple adjustment should clear it up and if not try using an absolute path. If neither of those suggestions work we'll do some debug work using the os.path module.

HTH

Ahah! Actually, as I typed that path out I realize what may be your mistake. In Python the \ is the escape character, so the interpreter automatically assumes any single backslashes designate an escape character (such as tab, newline, null, etc.). As a result you need to use \\ to indicate you actually want a backslash and not a special character.

You're right, a backslash is an escapecharacter. But I would recommend to change them into forward-slashes instead of double-backslash. It better-looking and you have less chance to make mistakes with it in the future: C:/Program Files/Mozilla Firefox/firefox.exe

My god! cheers guys :) I tried that once before but i don't think i did it for all of them I feel very stupid now :icon_redface: but thanks :)

Member Avatar for sravan953

Whoa! I learnt a lot from this thread! Kudos to all! =D

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.