10

Can python detect OS and then contruct a if/else statement for File System.

I would need to replace C:\CobaltRCX\ in Fn string with the FileSys string.

import os.path, csv
from time import strftime

if os.path.?????:## Windows
   FileSys = r"C:\\working\\" 
else:   ##linux   
   FileSys = r"\\working\\" 

y=(strftime("%y%m%d"))
Fn = (r"C:\\working\\Setup%s.csv" %y)
Merlin
  • 24,552
  • 41
  • 131
  • 206

10 Answers10

22

I usually just use this:

import os
if os.name == 'nt':
    pass # Windows
else:
    pass # other (unix)

edit:

Hopefully in response to your comments:

from time import strftime
import os

if os.name == 'nt': # Windows
    basePath = 'C:\\working\\'
else:
    basePath = '/working/'

Fn = '%sSetup%s.csv' % ( basePath, strftime( '%y%m%d' ) )
poke
  • 369,085
  • 72
  • 557
  • 602
  • 2
    Huh? What do you mean with that? – poke Jan 17 '11 at 23:44
  • The Fn string "C:\\working\\Setup%s.csv." How would 2nd variable. 1, the filesys the 2nd y the date. right now I have the date setup, need help with the string for "filesys" yada yada yada "date." thanks – Merlin Jan 17 '11 at 23:47
  • 1
    Not sure if I answer it with my edit now, but I really don't get it. Please try to explain it using comprehensive english otherwise... – poke Jan 18 '11 at 00:09
  • Yes, of course, sorry. You can also use os.path to concatenate multiple path sequences, if your path becomes more complex than this. – poke Jan 18 '11 at 08:15
6

see here: https://stackoverflow.com/a/58689984/3752715

import platform 
plt = platform.system()

if   plt == "Windows":   print("Your system is Windows")
elif plt == "Linux":     print("Your system is Linux")
elif plt == "Darwin":    print("Your system is MacOS")
else:                    print("Unidentified system")

you can see my github repo https://github.com/sk3pp3r/PyOS and use pyos.py script

Merlin
  • 24,552
  • 41
  • 131
  • 206
Haim Cohen
  • 281
  • 3
  • 5
5

Use sys.platform. You can find more information here http://docs.python.org/library/platform.html

Elalfer
  • 5,312
  • 20
  • 25
1

try this one:

    import platform
    platform.uname()

It works both on linux as well as windows. FYI: os.uname() will not work on windows, though it works on linux. Platform is generic.

Pawan Kumar
  • 2,132
  • 2
  • 16
  • 10
1

Yes.

>>> import os
>>> os.uname()
('Linux', 'ubuntu', '2.6.32-27-generic', '#49-Ubuntu SMP Thu Dec 2 00:51:09 UTC 2010', 'x86_64')
>>> system = os.uname()
>>> print system[0] + '/' + system[1]
Linux/ubuntu
>>> 
Andrew
  • 12,172
  • 16
  • 46
  • 61
1

Late answer, but if you're trying to determine the path separator, you can use

os.path.sep
A. Abramov
  • 1,823
  • 17
  • 45
0

You could look at os.uname

In [12]: os.uname()
Out[12]: 
('Darwin',
 'demitasse.local',
 '10.6.0',
 'Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386',
 'i386')
John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
0

For most usecases you should use the os.platform module. However if you need a more lean interface, try platinfo.

Michael
  • 8,920
  • 3
  • 38
  • 56
0

Here is what I just created the other day:

CODE:

def GetUserPlatform():
    if sys.platform == 'win32':
        UsrWinVer = str(sys.getwindowsversion().major)
        print("Operating System: Windows " + UsrWinVer)
    else:
        print("Something else")

GetUserPlatform()

OUTPUT:

Operating System: Windows 10

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
user1259765
  • 33
  • 1
  • 4
  • `platform.system()` is more consistent/reliable than `sys.platform`. See https://stackoverflow.com/a/1857/557406 – Charles L. Aug 14 '19 at 18:45
0
import platform
print(platform.uname().system)

This will give you Windows, Linux, etc.

Merlin
  • 24,552
  • 41
  • 131
  • 206
Manindhar
  • 171
  • 1
  • 4