detecting the operating system

W

Woojay Jeon

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.
 
R

Ray Smith

Woojay said:
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
 
P

Peter Otten

Woojay said:
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
 
C

Cameron Laird

P

Peter Hansen

Woojay said:
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
 
B

Bengt Richter

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
 
M

Michael

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.
 
J

James Kew

Michael said:
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
 
J

Josiah Carlson

Checking sys.platform is easy enough. I use it to figure out if I'm
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
 
J

James Kew

Josiah Carlson said:
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
 
R

Russell E. Owen

"Woojay Jeon said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top