Python判断操作系统类型代码分享

yipeiwu_com6年前Python基础

经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台(操作系统类型)。

代码如下:

复制代码 代码如下:

import platform

def TestPlatform():
    print ("----------Operation System--------------------------")
    #Windows will be : (32bit, WindowsPE)
    #Linux will be : (32bit, ELF)
    print(platform.architecture())

    #Windows will be : Windows-XP-5.1.2600-SP3 or Windows-post2008Server-6.1.7600
    #Linux will be : Linux-2.6.18-128.el5-i686-with-redhat-5.3-Final
    print(platform.platform())

    #Windows will be : Windows
    #Linux will be : Linux
    print(platform.system())

    print ("--------------Python Version-------------------------")
    #Windows and Linux will be : 3.1.1 or 3.1.3
    print(platform.python_version())

def UsePlatform():
  sysstr = platform.system()
  if(sysstr =="Windows"):
    print ("Call Windows tasks")
  elif(sysstr == "Linux"):
    print ("Call Linux tasks")
  else:
    print ("Other System tasks")
   
UsePlatform()

相关文章

解决pycharm界面不能显示中文的问题

主题的修改: File -> Settings , 弹出的窗口中: Appearance & Behavior -> Appearance , 可以修改“Theme”。 换成...

让python同时兼容python2和python3的8个技巧分享

python邮件列表里有人发表言论说“python3在10内都无法普及”。在我看来这样的观点有些过于悲观,python3和python2虽然不兼容,但他们之间差别并没很多人想像的那么大。...

python保存字符串到文件的方法

本文实例讲述了python保存字符串到文件的方法。分享给大家供大家参考。具体实现方法如下: def save(filename, contents): fh = open(fi...

Python遍历目录中的所有文件的方法

os.walk生成器 os.walk(PATH), PATH是个文件夹路径,当然可以用.或者../这样啦. 返回的是个三元元组为元素的列表, 每个元素代表了一个文件夹下的内容.第一个就是...

Python unittest单元测试框架的使用

一、测试模型 下面这部分来自于某书籍资料,拿过来,按需参考一下: 测试模型 (1)线性测试 1、概念: 通过录制或编写对应应用程序的操作步骤产生的线性脚本。单纯的来模拟用户完整的...