Python3语言基础
import sys
print ('参数个数为:', len(sys.argv), '个参数。')
print ('参数列表:', str(sys.argv))
import sys; x = ‘runoob’; sys.stdout.write(x + ‘\n’)
# 计算面积函数
def area(width, height):
return width * height
#!/usr/bin/python3
# 计算面积函数
def area(width, height):
return width * height
def print_welcome(name):
print("Welcome", name)
print_welcome("Runoob") w = 4 h = 5
print("width =", w, " height =", h, " area =", area(w, h))
#判断是否有一个目录
import os
dir = "/var/www/html/EnjoyCarApi/"
if os.path.isdir(dir):
print('%s is a dir' % dir)
else:
print('%s is not a dir' % dir)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018-12-17 17:16
# @Author : opsonly
# @Site :
# @File : systemissue.py
# @Software: PyCharm
# 系统内存与磁盘检测
import psutil
def memissue():
print('内存信息:')
mem = psutil.virtual_memory()
# 单位换算为MB
memtotal = mem.total/1024/1024
memused = mem.used/1024/1024
membaifen = str(mem.used/mem.total*100) + '%'
print('%.2fMB' % memused)
print('%.2fMB' % memtotal)
print(membaifen)
def cuplist():
print('磁盘信息:')
disk = psutil.disk_partitions()
diskuse = psutil.disk_usage('/')
#单位换算为GB
diskused = diskuse.used / 1024 / 1024 / 1024
disktotal = diskuse.total / 1024 / 1024 / 1024
diskbaifen = diskused / disktotal * 100
print('%.2fGB' % diskused)
print('%.2fGB' % disktotal)
print('%.2f' % diskbaifen)
memissue()
print('*******************')
cuplist()
复制代码
# 查看网段内有多少IP地址
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018-12-18 15:31
# @Author : opsonly
# @Site :
# @File : ipTest.py
# @Software: PyCharm
import IPy
ip = IPy.IP('172.16.0.0/26')
print(ip.len())
for i in ip:
print(i)
复制代码
获取系统当前时间
import sys
import time
time_str = time.strftime("日期:%Y-%m-%d",time.localtime())
print(time_str)
time_str= time.strftime("时间:%H:%M",time.localtime())
print(time_str)
#!/usr/bin/python3
str='Runoob'
print(str) # 输出字符串
print(str[0:-1]) # 输出第一个到倒数第二个的所有字符
print(str[0]) # 输出字符串第一个字符
print(str[2:5]) # 输出从第三个开始到第五个的字符
print(str[2:]) # 输出从第三个开始后的所有字符
print(str * 2) # 输出字符串两次
print(str + '你好') # 连接字符串
print('------------------------------')
print('hello\nrunoob') # 使用反斜杠(\)+n转义特殊字符
print(r'hello\nrunoob') # 在字符串前面添加一个 r,表示原始字符串,不会发生转义