LoginSignup
21
25

More than 5 years have passed since last update.

Pythonを実行しているOSの判別

Posted at

Pythonのプログラムで、ファイル名をフルパスで指定する場合など、WindowsとLinuxでパス名が違っているため区別したいことがあります。

WindowsかLinuxかを判別するだけでよければ、os.nameで十分です。

Windowsの場合

>>> import os
>>> os.name
'nt'

Bash on Ubuntu on WindowsなどLinuxの場合

>>> import os
>>> os.name
'posix'

となるので、os.nameをチェックすれば、どちらのOSかがわかります。

判別例

if os.name == 'nt': fname = 'C:\Windows\Fonts\YuGothM.ttc'
else: fname = '/mnt/c/Windows/Fonts/YuGothM.ttc'
21
25
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
25