Can python detect which OS is it running under?

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:workingSetup%s.csv" %y)

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

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


是。

>>> 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
>>> 
链接地址: http://www.djcxy.com/p/3376.html

上一篇: 如何在没有Python中的路径扩展的情况下获取文件名?

下一篇: python能检测到它运行的是哪个操作系统?