473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

detecting the operating system

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.
Jul 18 '05 #1
11 4332
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
Jul 18 '05 #2
"Ray Smith" <ra*@rays-web.com> wrote in message
news:40******@news.syd.ip.net.au...
Woojay Jeon wrote: - SNIP -
sys.platform

Ray Smith


Oops, that was quite a dumbass question, don't know how I missed that.
Thanks!
Jul 18 '05 #3
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
Jul 18 '05 #4
In article <c1**********@news-int.gatech.edu>,
Woojay Jeon <wj************@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).

Jul 18 '05 #5
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
Jul 18 '05 #6
On Fri, 27 Feb 2004 03:00:31 -0500, "Woojay Jeon" <wj************@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
Jul 18 '05 #7
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.


Jul 18 '05 #8
"Michael" <mo*****@mlug.missouri.edu> wrote in message
news:ma*************************************@pytho n.org...
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

Jul 18 '05 #9
>>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
Jul 18 '05 #10
"Josiah Carlson" <jc******@nospam.uci.edu> wrote in message
news:c1**********@news.service.uci.edu...
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
Jul 18 '05 #11
In article <c1**********@news-int.gatech.edu>,
"Woojay Jeon" <wj************@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.


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
Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jive | last post by:
The short form of the question is, how can I trace memory usage (preferably from C) with an eye toward finding out if I'm leaking memory? I would have thought there would be a ton of info on...
9
by: Kevin Frey | last post by:
Hello, Can anyone suggest a way, that does not involve writing a temporary file, that would permit me to discover whether writing: output_file_stream << '\n'; will write either a LF (eg....
9
by: prabhat143 | last post by:
Hi, I was recently asked to write a function in C that would detect if memory is corrupted. I had no clue about the solution but what I believe is that the solution is not complicated. Does...
7
by: fox | last post by:
Maybe this is not the best group to ask this question, but I don't know a better one. I'm looking for a *portable* program in C (I mean source code) to detect whether unaligned word access is:...
4
by: rodmc | last post by:
I have written an application which works perfectly when the machine is operating under normal conditions, however when the screen becomes locked it imediately starts to fill up several hundred...
8
by: dwelch91 | last post by:
I need to detect whether the operating system I am running on (not the Python version) is 64bit or 32bit. One requirement is that I need to include support for non-Intel/AMD architectures. The 2...
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
4
by: Devraj | last post by:
Hi everyone, My Python program needs reliably detect which Operating System its being run on, infact it even needs to know which distribution of say Linux its running on. The reason being its a...
44
by: Pilcrow | last post by:
Is there a way that a proram can detect whether it is operating in an ASCII or an EBCDIC environment?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.