detecting the operating system

Discussion in 'Python' started by Woojay Jeon, Feb 27, 2004.

  1. Woojay Jeon

    Woojay Jeon Guest

    OK, I tried a Google search on this Usenet group but couldn't find a
    solution, so I'm posting my question here (if there's a better archive than
    the one in Google, please let me know).

    Does anybody know how to detect the operating system under which the current
    Python program is running, especially whether it's Windows or Unix? I have a
    program that needs to search for files in "c:\test" if it's running under
    Windows, and "/home/user/test" if it's running under Unix, so the simplest
    solution I can think of is to detect the operating system, but if anyone
    could suggest a workaround, that would also be fine.
    Woojay Jeon, Feb 27, 2004
    #1
    1. Advertising

  2. Woojay Jeon

    Ray Smith Guest

    Woojay Jeon wrote:
    > OK, I tried a Google search on this Usenet group but couldn't find a
    > solution, so I'm posting my question here (if there's a better archive than
    > the one in Google, please let me know).
    >
    > Does anybody know how to detect the operating system under which the current
    > Python program is running, especially whether it's Windows or Unix? I have a
    > program that needs to search for files in "c:\test" if it's running under
    > Windows, and "/home/user/test" if it's running under Unix, so the simplest
    > solution I can think of is to detect the operating system, but if anyone
    > could suggest a workaround, that would also be fine.


    sys.platform

    Ray Smith
    Ray Smith, Feb 27, 2004
    #2
    1. Advertising

  3. Woojay Jeon

    Woojay Jeon Guest

    "Ray Smith" <> wrote in message
    news:...
    > Woojay Jeon wrote:

    - SNIP -
    >
    > sys.platform
    >
    > Ray Smith
    >


    Oops, that was quite a dumbass question, don't know how I missed that.
    Thanks!
    Woojay Jeon, Feb 27, 2004
    #3
  4. Woojay Jeon

    Peter Otten Guest

    Woojay Jeon wrote:

    > Does anybody know how to detect the operating system under which the
    > current Python program is running, especially whether it's Windows or
    > Unix? I have a program that needs to search for files in "c:\test" if it's
    > running under Windows, and "/home/user/test" if it's running under Unix,
    > so the simplest solution I can think of is to detect the operating system,
    > but if anyone could suggest a workaround, that would also be fine.


    Instead of sys.platform, os.name might be helpful as it is less specific. I
    think you will get os.name == "posix" for Unix/Linux etc. and os.name ==
    "nt" for win32.

    Peter
    Peter Otten, Feb 27, 2004
    #4
  5. In article <c1mtcj$ofv$>,
    Woojay Jeon <wjj{nospam}> wrote:
    >OK, I tried a Google search on this Usenet group but couldn't find a
    >solution, so I'm posting my question here (if there's a better archive than
    >the one in Google, please let me know).

    .
    .
    .
    No; Google's best.

    I write that as the expert on newsgroup archives <URL:
    http://phaseit.net/claird/news.lists/newsgroup_archives.html >.
    Google's simply on top. For some purposes, though, you might find
    <URL: http://mail.python.org/pipermail/python-list/ > handier.
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Cameron Laird, Feb 27, 2004
    #5
  6. Woojay Jeon

    Peter Hansen Guest

    Woojay Jeon wrote:
    >
    > OK, I tried a Google search on this Usenet group but couldn't find a
    > solution, so I'm posting my question here (if there's a better archive than
    > the one in Google, please let me know).
    >
    > Does anybody know how to detect the operating system under which the current
    > Python program is running, especially whether it's Windows or Unix? I have a
    > program that needs to search for files in "c:\test" if it's running under
    > Windows, and "/home/user/test" if it's running under Unix, so the simplest
    > solution I can think of is to detect the operating system, but if anyone
    > could suggest a workaround, that would also be fine.


    Would it be somewhat easier just to pass in the directory as a command
    line argument? That way you (a) don't need to have hardcoded paths, (b)
    don't need the platform-detection logic, and (c) can more easily change
    it in the inevitable case that it needs to change someday.

    -Peter
    Peter Hansen, Feb 27, 2004
    #6
  7. On Fri, 27 Feb 2004 03:00:31 -0500, "Woojay Jeon" <wjj{nospam}> wrote:

    >OK, I tried a Google search on this Usenet group but couldn't find a
    >solution, so I'm posting my question here (if there's a better archive than
    >the one in Google, please let me know).
    >
    >Does anybody know how to detect the operating system under which the current
    >Python program is running, especially whether it's Windows or Unix? I have a
    >program that needs to search for files in "c:\test" if it's running under
    >Windows, and "/home/user/test" if it's running under Unix, so the simplest
    >solution I can think of is to detect the operating system, but if anyone
    >could suggest a workaround, that would also be fine.
    >

    Why not just

    dirpath = r'c:\test'
    if not os.path.isdir(dirpath):
    dirpath = '/home/user/test' # ~/test ?? or is there an account named "user"?
    # or maybe '/home/%s/test'%os.popen('whoami').read().strip() #untested

    IWT it's fairly safe to assume the windows path is not going to exist on unix
    unless something strange is going on. This way you don't have to worry about
    different flavors of unix and windows.

    Regards,
    Bengt Richter
    Bengt Richter, Feb 28, 2004
    #7
  8. Woojay Jeon

    Michael Guest

    Checking sys.platform is easy enough. I use it to figure out if I'm
    running on Linux or Windows. For Linux I take the users home directory
    to put files in. In Windows I check the registry to find out the
    location of My Documents and then save files there. Checking the Windows
    registry is a little more work but can make your programs work quite
    nicely in Windows.

    >OK, I tried a Google search on this Usenet group but couldn't find a
    >solution, so I'm posting my question here (if there's a better archive than
    >the one in Google, please let me know).
    >
    >Does anybody know how to detect the operating system under which the current
    >Python program is running, especially whether it's Windows or Unix? I have a
    >program that needs to search for files in "c:\test" if it's running under
    >Windows, and "/home/user/test" if it's running under Unix, so the simplest
    >solution I can think of is to detect the operating system, but if anyone
    >could suggest a workaround, that would also be fine.
    >
    Michael, Feb 28, 2004
    #8
  9. Woojay Jeon

    James Kew Guest

    "Michael" <> wrote in message
    news:...
    > Checking sys.platform is easy enough. I use it to figure out if I'm
    > running on Linux or Windows. For Linux I take the users home directory
    > to put files in. In Windows I check the registry to find out the
    > location of My Documents and then save files there.


    os.path.expanduser("~") ?

    James
    James Kew, Feb 28, 2004
    #9
  10. >>Checking sys.platform is easy enough. I use it to figure out if I'm
    >>running on Linux or Windows. For Linux I take the users home directory
    >>to put files in. In Windows I check the registry to find out the
    >>location of My Documents and then save files there.

    >
    >
    > os.path.expanduser("~") ?


    Doesn't work for all platforms. Occasionally in windows that will
    expand to %USERPROFILE%, which you then have to resolve with os.getenv.

    - Josiah
    Josiah Carlson, Feb 29, 2004
    #10
  11. Woojay Jeon

    James Kew Guest

    "Josiah Carlson" <> wrote in message
    news:c1thq4$129$...
    > > os.path.expanduser("~") ?

    >
    > Doesn't work for all platforms. Occasionally in windows that will
    > expand to %USERPROFILE%, which you then have to resolve with os.getenv.


    I'm also told (in email) it doesn't work under Win98.

    Which is a shame, 'cause it's quite handy...

    James
    James Kew, Mar 1, 2004
    #11
  12. In article <c1mtcj$ofv$>,
    "Woojay Jeon" <wjj{nospam}> wrote:

    >OK, I tried a Google search on this Usenet group but couldn't find a
    >solution, so I'm posting my question here (if there's a better archive than
    >the one in Google, please let me know).
    >
    >Does anybody know how to detect the operating system under which the current
    >Python program is running, especially whether it's Windows or Unix? I have a
    >program that needs to search for files in "c:\test" if it's running under
    >Windows, and "/home/user/test" if it's running under Unix, so the simplest
    >solution I can think of is to detect the operating system, but if anyone
    >could suggest a workaround, that would also be fine.


    To simply differentiate between Windows and everything else, try
    importing a Windows-only library, such as _winreg or winsound:

    try:
    import _winreg
    # at this point it's some flavor of Windows
    except ImportError:
    # not Windows

    -- Russell
    Russell E. Owen, Mar 2, 2004
    #12
    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. Vasanth
    Replies:
    0
    Views:
    532
    Vasanth
    Jun 28, 2004
  2. Steven Cheng[MSFT]
    Replies:
    4
    Views:
    4,307
    Steven Cheng[MSFT]
    May 13, 2004
  3. Karunakararao
    Replies:
    0
    Views:
    631
    Karunakararao
    May 11, 2004
  4. =?Utf-8?B?U3JlZQ==?=
    Replies:
    1
    Views:
    337
    Ken Cox [Microsoft MVP]
    May 31, 2004
  5. Karunakararao
    Replies:
    1
    Views:
    345
    Michael Kremser
    Sep 6, 2004
Loading...

Share This Page