Launching Wordpad on Windows

Discussion in 'Python' started by Patrick L. Nolan, Jan 20, 2004.

  1. I'm trying to find a clean way to launch a Wordpad editor
    on Windows. By "clean", I mean that it should work on as
    many versions of Windows as possible, and it shouldn't
    require installing any extra software. I assume everyone
    has win32api and its friends.

    The problem is to find the path to wordpad.exe. At first
    I just copied the path from my XP machine:
    c:\Program Files\Windows NT\Accessories\WORDPAD.EXE

    I verified that the same path works on W2K, but I don't
    know about older versions. With some labor I was able
    to come up with a longer, more obfuscated bit of code:

    item = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\WORDPAD.EXE"
    key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, item, 0,
    win32con.KEY_QUERY_VALUE)
    info = win32api.RegQueryValueEx(key, None)
    win32api.RegCloseKey(key)
    editor = win32api.ExpandEnvironmentStrings(info[0])

    I would like to solicit learned opinions about this. Which
    version will work in more versions of Windows? Is there a
    better approach?

    --
    * Patrick L. Nolan *
    * W. W. Hansen Experimental Physics Laboratory (HEPL) *
    * Stanford University *
     
    Patrick L. Nolan, Jan 20, 2004
    #1
    1. Advertising

  2. Patrick L. Nolan

    Adonis Guest

    "Patrick L. Nolan" <> wrote in message
    news:buhtvp$l0j$...
    > I'm trying to find a clean way to launch a Wordpad editor
    > on Windows. By "clean", I mean that it should work on as
    > many versions of Windows as possible, and it shouldn't
    > require installing any extra software. I assume everyone
    > has win32api and its friends.
    >
    > The problem is to find the path to wordpad.exe. At first
    > I just copied the path from my XP machine:
    > c:\Program Files\Windows NT\Accessories\WORDPAD.EXE
    >
    > I verified that the same path works on W2K, but I don't
    > know about older versions. With some labor I was able
    > to come up with a longer, more obfuscated bit of code:
    >
    > item = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App

    Paths\\WORDPAD.EXE"
    > key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, item, 0,
    > win32con.KEY_QUERY_VALUE)
    > info = win32api.RegQueryValueEx(key, None)
    > win32api.RegCloseKey(key)
    > editor = win32api.ExpandEnvironmentStrings(info[0])
    >
    > I would like to solicit learned opinions about this. Which
    > version will work in more versions of Windows? Is there a
    > better approach?
    >
    > --
    > * Patrick L. Nolan *
    > * W. W. Hansen Experimental Physics Laboratory (HEPL) *
    > * Stanford University *



    This works for me:
    (Using WinXP Pro)

    Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.system("start wordpad")

    0

    Hope this helps.

    Adonis
     
    Adonis, Jan 20, 2004
    #2
    1. Advertising

  3. > > I'm trying to find a clean way to launch a Wordpad
    > > editor on Windows. By "clean", I mean that it should
    > > work on as many versions of Windows as possible,
    > > and it shouldn't require installing any extra software.
    > > I assume everyone has win32api and its friends.
    > >
    > > The problem is to find the path to wordpad.exe. At
    > > first I just copied the path from my XP machine:
    > > c:\Program Files\Windows NT\Accessories\WORDPAD.EXE
    > >
    > > I verified that the same path works on W2K, but I
    > > don't know about older versions.


    Never hard code a path like that. It definitely won't work on all versions
    of Windows. It won't even work on all XP systems. The Program Files
    directory isn't always on the C: drive!

    > > With some labor I was able to come up with a
    > > longer, more obfuscated bit of code:
    > >
    > > [code using the App Paths registry key]
    > >
    > > I would like to solicit learned opinions about this.
    > > Which version will work in more versions of
    > > Windows? Is there a better approach?


    You can do it with App Paths, but that's way too much work. It's not the
    Windowsonic way to do it. :)

    > This works for me:
    > (Using WinXP Pro)
    >
    > >>> import os
    > >>> os.system("start wordpad")

    > 0
    >
    > Hope this helps.


    I wouldn't recommend os.system. The problem is that it starts a command
    shell. You didn't notice this because you're testing from a console window,
    so the secondary command shell runs in the same console window you're
    already using. But if you put this code in a non-console app, it will open a
    new console window in addition to the WordPad window. That window will close
    right away because you're using the 'start' command, but it's extra screen
    activity you don't want.

    So what's the right way to do it? The underlying Windows function you're
    looking for is ShellExecute(). That's the easiest way to launch an app. It
    knows all about App Paths and the environment PATH. It's the same function
    that gets called if you do a Start/Run... in the Windows UI.

    You can use win32api.ShellExecute, or better yet, the os.startfile function
    calls ShellExecute, so you don't even need win32api:

    >>> import os
    >>> os.startfile('wordpad.exe')


    -Mike
     
    Michael Geary, Jan 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wilk Teverbaugh
    Replies:
    0
    Views:
    430
    Wilk Teverbaugh
    Dec 15, 2003
  2. Mickey Segal
    Replies:
    18
    Views:
    11,479
    Andrew Thompson
    Dec 9, 2005
  3. Luigi Donatello Asero

    Re: Write Chinese in Wordpad

    Luigi Donatello Asero, Sep 24, 2005, in forum: HTML
    Replies:
    11
    Views:
    1,335
    Luigi Donatello Asero
    Sep 26, 2005
  4. Luigi Donatello Asero

    Re: Write Chinese in Wordpad

    Luigi Donatello Asero, Sep 26, 2005, in forum: HTML
    Replies:
    0
    Views:
    481
    Luigi Donatello Asero
    Sep 26, 2005
  5. Alexandre Badez

    Launching command on windows

    Alexandre Badez, Sep 27, 2007, in forum: Python
    Replies:
    4
    Views:
    350
Loading...

Share This Page