h0945466 发表于 2018-8-8 14:01:43

python 题目3

  磁盘使用情况
  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   >  udev            1.5G   01.5G   0% /dev
  tmpfs         300M9.3M290M   4% /run
  /dev/sda1      21G   13G7.5G62% /
  tmpfs         1.5G248K1.5G   1% /dev/shm
  tmpfs         5.0M4.0K5.0M   1% /run/lock
  tmpfs         1.5G   01.5G   0% /sys/fs/cgroup
  /dev/sdb1      30G3.1G   25G11% /data
  tmpfs         300M   0300M   0% /run/user/0
  tmpfs         300M   0300M   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()
  list.append(ip)
  list_num = set(list)
  for j in list_num:
  num = list.count(j)
  print '%s : %s' %(j,num)
页: [1]
查看完整版本: python 题目3