Python peut-il détecter sous quel OS tourne-t-il?

peut Python détecter OS et ensuite contruire une instruction if/else pour le système de fichiers.

j'aurais besoin de remplacer C:CobaltRCX dans Fn chaîne avec le FileSys chaîne.

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)
2
demandé sur Merlin 2011-01-18 02:34:38

7 réponses

Je n'utilise habituellement que ceci:

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

edit:

avec un peu de chance en réponse à vos commentaires:

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' ) )
9
répondu poke 2011-01-18 08:14:37

utiliser sys.platform . Vous pouvez trouver plus d'informations ici http://docs.python.org/library/platform.html

4
répondu Elalfer 2011-01-17 23:36:48

Oui.

>>> 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
répondu Andrew 2011-01-17 23:41:00

pour la plupart des usecases, vous devez utiliser le module os.platform . Cependant, si vous avez besoin d'une interface plus légère, essayez platinfo .

1
répondu Michael 2011-01-17 23:42:25

essayez celui-ci:

    import platform
    platform.uname()

il fonctionne à la fois sur linux et windows. FYI: os.uname () ne fonctionnera pas sous windows, bien qu'il fonctionne sous linux. La plateforme est générique.

1
répondu Pawan Kumar 2014-08-21 06:55:01

vous pouvez regarder 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
répondu John Percival Hackworth 2011-01-17 23:39:48

voici ce que je viens de créer l'autre jour:

CODE:

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

GetUserPlatform()

sortie:

Système D'Exploitation: Windows 10

0
répondu user1259765 2018-04-06 16:41:25