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)
2

10 Answers 10

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' ) )
4
  • 2
    Huh? What do you mean with that?
    – poke
    Jan 17, 2011 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, 2011 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, 2011 at 0: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, 2011 at 8: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

5

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

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

1

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

os.path.sep
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')
0

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

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

1
0
import platform
print(platform.uname().system)

This will give you Windows, Linux, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.