Python 官方文档:入门教程 => 点击学习
磁盘使用情况root@ubuntu:/data/server/spider/面试题# cat 02check_disk.py import timeimport osnew_time = time.strftime('%Y-%m-%d')p
磁盘使用情况
root@ubuntu:/data/server/spider/面试题# cat 02check_disk.py
import time
import os
new_time = time.strftime('%Y-%m-%d')
print(new_time)
disk_status = os.popen('df -h').readlines()
str1 =''.join(disk_status)
with open(new_time+'.log','w') as f:
f.write('%s' % str1)
f.flush()
f.close()
root@ubuntu:/data/server/spider/面试题# cat 2017-11-21.log
Filesystem Size Used Avail Use% Mounted on
udev 1.5G 0 1.5G 0% /dev
tmpfs 300M 9.3M 290M 4% /run
/dev/sda1 21G 13G 7.5G 62% /
tmpfs 1.5G 248K 1.5G 1% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 1.5G 0 1.5G 0% /sys/fs/cgroup
/dev/sdb1 30G 3.1G 25G 11% /data
tmpfs 300M 0 300M 0% /run/user/0
tmpfs 300M 0 300M 0% /run/user/1000
四、生成磁盘使用情况的日志文件
#!/usr/bin/env python
#!coding=utf-8
import time
import os
new_time = time.strftime('%Y-%m-%d')
disk_status = os.popen('df -h').readlines()
str1 = ''.join(disk_status)
f = file(new_time+'.log','w')
f.write('%s' % str1)
f.flush()
f.close()
五、统计出每个IP的访问量有多少?(从日志文件中查找)
#!/usr/bin/env Python
#!coding=utf-8
list = []
f = file('/tmp/1.log')
str1 = f.readlines()
f.close()
for i in str1:
ip = i.split()[0]
list.append(ip)
list_num = set(list)
for j in list_num:
num = list.count(j)
print '%s : %s' %(j,num)
--结束END--
本文标题: python 题目3
本文链接: https://lsjlt.com/news/192320.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0