#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    15

    Launching applications using Python


    Hello,

    Is there anyway to launch the windows application from within a python script. I mean when I want to launch an application on my computer desktop, I double click on it. But is there anyway to do that by a python script?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    there are several ways to do it.

    The simplest is to use os.system(command) to run an external command. This will launch an application - either the executable needs to be on the path, or you need to specify the full path.

    e.g.

    Code:
    #run the windows calculator:
    import os
    os.system('calc.exe')
    The system function will normally wait until the application has been closed before returning, which may or may not be what you want. If you want to launch an application and have the python script continue, then you can use the Windows start command:

    Code:
    #launch the windows calculator and continue
    import os
    os.system('start calc.exe')
    If you want to run a command line program and return the output to python you will need to use one of the popen functions. That is a much bigger topic, so I will not cover it here.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Andorra / England
    Posts
    80
    Rep Power
    11
    Hi, im just trying to fathom out some things for a cdrecord gui. Can this command be used the same way with a linux shell? And would it take extra parameters including strings. ie "nano -s $myfile"
    If there is some documentation, im sure i can find everything else i need. Sorry if my lingo isnt quite right, (is it a string in python?) but im sure you know what i mean.

    Thanks.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    os.system() will work regardless of platform, just like in C/C++ if you're familiar with any of these. Of course the command you need to issue will probably be dependent on the platform the program is running on. Oh, and yes: it is called a string in Python.

    check out the os module: http://www.python.org/doc/2.3.4/lib/module-os.html; you might also want to check out the sys module i.e. you can check the platform Python is running on using sys.platform.

    >>> import sys
    >>> sys.platform
    'darwin'
    >>>
    Mark.
    Last edited by netytan; October 5th, 2004 at 05:47 PM.
    programming language development: www.netytan.com Hula

  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Passing a parameter to notepad using a string:
    Code:
    >>> import os
    >>> fname = "readme.txt"
    >>> os.system("notepad"+" " + fname)
    You do the same with any command that takes command line parameters (on any platform). Note that you may get a console window (xterm) - if this is not what you want then checkout the process spawning commands like spawnv and popen (see python help files for details) A search of this forum will probably show some examples.

    grim
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    27
    Rep Power
    0
    Another option besides os.system is os.startfile.

    os.startfile is identical to clicking on a file (or application) in Windows Explorer or the desktop. By supplying the os.startfile function with the file name, it will open that file in the appropriate application. I'm not sure if it will work under Linux or not.

    For further information, see the doc string for the startfile function.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    os.system! I knew there was a tidier way than os.spawnv() the last time I was trying to remember how to launch programs, but I couldn't remember it. Oh well.

IMN logo majestic logo threadwatch logo seochat tools logo