detecting the operating system

Bengt Richter bokr at oz.net
Fri Feb 27 21:35:36 EST 2004


On Fri, 27 Feb 2004 03:00:31 -0500, "Woojay Jeon" <wjj{nospam}eon at ece.gatech.edu> 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



More information about the Python-list mailing list