Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > detecting the operating system

Reply
Thread Tools

detecting the operating system

 
 
Woojay Jeon
Guest
Posts: n/a
 
      02-27-2004
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.


 
Reply With Quote
 
 
 
 
Ray Smith
Guest
Posts: n/a
 
      02-27-2004
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


 
Reply With Quote
 
 
 
 
Woojay Jeon
Guest
Posts: n/a
 
      02-27-2004
"Ray Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Woojay Jeon wrote:

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


Oops, that was quite a dumbass question, don't know how I missed that.
Thanks!


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      02-27-2004
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
 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      02-27-2004
In article <c1mtcj$ofv$(E-Mail Removed)>,
Woojay Jeon <wjj{nospam}(E-Mail Removed)> 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..._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 <(E-Mail Removed)>
Business: http://www.Phaseit.net
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      02-27-2004
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
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      02-28-2004
On Fri, 27 Feb 2004 03:00:31 -0500, "Woojay Jeon" <wjj{nospam}(E-Mail Removed)> 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
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      02-28-2004
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.
>


 
Reply With Quote
 
James Kew
Guest
Posts: n/a
 
      02-28-2004
"Michael" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Josiah Carlson
Guest
Posts: n/a
 
      02-29-2004
>>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
connecting both computers with different operating system together naderbd Wireless Networking 1 07-29-2005 12:47 AM
Sun to Give Out Operating System for Free Rich Firefox 7 11-16-2004 07:47 PM
How to get the Operating System info like ( Wireless info, Wireless connection) Vasanth Perl 0 06-28-2004 08:56 AM
Re: 32 bit operating system Consultant MCSE 0 01-08-2004 02:58 PM
Re: 32 bit operating system Politician Spock MCSE 0 01-08-2004 02:55 PM



Advertisments