• ##python案例##


    一、文件目录案例

    Ⅰ、文件目录按关键词备份迁移

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import  os
    import shutil
    #
    # 处理文件及目录
    # @a_srcPath 目录路径
    # # copyOrMove 复制操作还是移动操作 False为复制操作,True为移动操作
     
    allfile = []
    def handle_dir(a_srcPath, a_findKey, a_copyOrMove=False):
        srcPathList = os.listdir(a_srcPath)
        #print(str(srcPathList))
        for sPath in srcPathList:
            filepath = os.path.join(a_srcPath, sPath)
            # print("filepath="+filepath)
            # 判断是目录
            if os.path.isdir(filepath):
              handle_dir(filepath, a_findKey, a_copyOrMove)
              if filepath.find(a_findKey)>=0:
                 len_asrcPath = len(srcPath_root)
                 destPath= destPath_root+filepath[len_asrcPath:]
                 # destPath = filepath
                 #print("destPath="+destPath)
                 #print("filepath=" + filepath)
                 allfile.append(destPath)
                 if a_copyOrMove:  
                     shutil.move(filepath, destPath)    #移动目录
                 else:
                     shutil.copytree(filepath, destPath) #复制目录
     
        return allfile
     
    findKey='2018-'
    srcPath_root='/home/mysoft/attachments'
    destPath_root='/home/mysoft/bk/attachments_history/'+findKey
     
    allfiles=handle_dir(srcPath_root,findKey,False)
     
    for item in allfiles:
       print(item)
    

    Ⅱ、日志切割

    import os, sys, datetime,re
    
    # nginx日志存放的路径
    nginxLogPath="/opt/nginx-1.9.5/logs/"
    # 获取昨天的日期
    yesterday = (datetime.date.today() + datetime.timedelta(days = -1)).strftime("%Y-%m-%d")
    # nginx启动的pid文件
    PID = "/var/run/nginx.pid"
    
    def cutNginxLog(path):
        """
        切割nginx日志函数
        :param path: 日志文件的第一级目录
        :return: 
        """
        logList = os.listdir(path)    # 判断传入的path是否是目录
        for logName in logList:      # 循环处理目录里面的文件
            logAbsPath = os.path.join(path, logName)
            if os.path.isdir(logAbsPath):   # 如果是目录,递归调用自己,继续处理
                cutNginxLog(logAbsPath)
            else:         # 如果是日志文件,就进行处理
                # 分割日志
                re_Num = re.compile(r'^[a-zA-Z]')
                # 判断日志文件是否是字母开头,如果是日期开头就不切割
                if re_Num.search(logName):
                    logNewName = yesterday + "_" + logName          # 新日志文件名称列如:2018-11-8_access.log
                    oldLogPath = os.path.join(path, logName)        # 旧日志文件绝对路径
                    newLogPath = os.path.join(path, logNewName)     # 新日志文件绝对路径
                    os.rename(oldLogPath, newLogPath)
    
        cmd = " kill -USR1 `cat %s` "% PID
        res = os.system(cmd)
        if res != 0:
            return "重新加载nginx失败"
    cutNginxLog(nginxLogPath)
    

    二、监控案例

    Ⅰ:监控端口

    #验证端口是否正常<br>import socket
     
    def get_ports(port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex(('127.0.0.1',port))
        print(result)  #输出返回码
        if result != 0:
           send_data = "服务器的%d",port,'端口挂了,快去修复哈'
           print (send_data)
        else:
           print("端口:",port,"正常")
    

      

  • 相关阅读:
    Java_swing控件实例
    java_泛型 TreeSet 判断hashcode/length(升序排列)
    java_泛型(设置通配符下限)
    java_泛型(构造器)部分实例
    子类可以继承的同时实现接口
    继承类的线程写法
    匿名内部类的线程写法
    接口作为参数并用参数变量可以调用接口中的方法------------------需要多练习
    类可以作为参数类型,参数的变量还能调用作为参数类型的方法--------------需要多练习
    接口和类
  • 原文地址:https://www.cnblogs.com/wuchangblog/p/12844848.html
Copyright © 2020-2023  润新知