who命令:列出所有登陸當前系統(tǒng)中的用戶信息
作用:保存一些用戶登錄信息蜓耻,諸如登陸名、用戶登錄的終端類型饶氏、用戶登錄的時間和地點。
使用正則語法:
\s:匹配任何空格字符
\t:匹配一個制表符疹启。等價于\x09和\cI蔼卡。
+:匹配1次或多次前面出現(xiàn)得到正則表達式
使用模塊及函數(shù):
re模塊
re.split(pattren,string,max=0):根據(jù)正則表達式的模式分隔符,split函數(shù)將字符串分割為列表荤懂,然后返回成功匹配的列表,分隔最多操作max次
strip():方法用于移除字符串頭尾指定的字符(默認為空格)节仿。
os模塊
os.popen(command[,mode[,bufsize]]):從一個 command 打開一個管道,函數(shù)返回一個file對象
#當然os.popen()命令已經(jīng)被subpress模塊替代了
python2版本
import re
import os
with os.popen('who','r') as f:
? ? for eachLine in f:
? ? ? ? print(re.split(r'\s\s+|\t',eachLine.strip()))
python3版本
import os
import re
with os.popen('who','r') as f:
? ? for eachLine in f:
? ? ? ? print(re.split(r'\s\s+|\t',eachLine.strip()))
python通用版本
import os
import re
from distutils.log import warn as printf
with os.popen('who','r') as f:
? ? for eachLine in f:
? ? ? ? printf(re.split(r'/s/s+|/t',eachLine.strip()))