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)
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' ) )
utiliser sys.platform
. Vous pouvez trouver plus d'informations ici http://docs.python.org/library/platform.html
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
>>>
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
.
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.
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')
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