码迷,mamicode.com
首页 > 编程语言 > 详细

python 题目3

时间:2017-11-21 23:49:06      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:python面试题

磁盘使用情况

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)


本文出自 “梦女孩” 博客,请务必保留此出处http://dreamgirl1314.blog.51cto.com/1159474/1983944

python 题目3

标签:python面试题

原文地址:http://dreamgirl1314.blog.51cto.com/1159474/1983944

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!