阅读背景:

Python运维开发基础09-函数基础

来源:互联网 

上节作业回顾

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# author:Mr.chen
# 实现简单的shell命令sed的替换功能

import sys,os

para_argv = sys.argv
Tag = True

#判断参数的传递
if para_argv[1] == "-i" and len(para_argv) == 4 :
    if type(para_argv[2]) == str :
        if os.path.isfile(para_argv[3]) and os.popen("whoami").read().strip() == "root" :
            #对过滤条件的字符串进行分割,请注意此处写法,可以降低判断嵌套深度
            List = []
            if "#" in para_argv[2] and para_argv[2].count("#") == 3 :
                List = para_argv[2].split("#")
            elif "@" in para_argv[2] and para_argv[2].count("@") == 3:
                List = para_argv[2].split("@")
            elif "/" in para_argv[2] and para_argv[2].count("/") == 3:
                List = para_argv[2].split("/")
            else :
                print ("请用@或/或#分隔过滤条件,分隔符只能是3个")
            #对分割以后的列表进行判断
            if len(List) != 0 and List[0].strip() == "s" :
                if List[3].strip() != "" and List[3] != "g" :
                    print ("{}是个无法识别的字符".format(List[3]))
                else :
                    with open(para_argv[3],"r") as f , open(para_argv[3]+".bak","w") as f_new :
                        for line in f :
                            if List[1] in line and List[3].strip() == "" :
                                line = line.replace(List[1],List[2],1)
                            else :
                                line = line.replace(List[1],List[2],-1)
                            f_new.write(line)
                    os.system("/bin/mv "+para_argv[3]+".bak "+para_argv[3])
                    print ("命令执行成功!")
            elif len(List) != 0 and List[0].strip().count("s") == 1 and List[0].strip().endswith("s") :
                if List[0].strip().count(",") == 1 :
                    if List[0].strip().split(",")[0].isdigit() and List[0].strip().split(",")[1][:-1].isdigit() :
                        num_one = int(List[0].strip().split(",")[0])
                        num_two = int(List[0].strip().split(",")[1][:-1])
                        with open(para_argv[3],"r") as f , open(para_argv[3]+".bak","w") as f_new :
                            for index,line in enumerate(f) :
                                if num_one <= index+1 <= num_two :
                                    if List[1] in line and List[3].strip() == "" :
                                        line = line.replace(List[1],List[2],1)
                                    elif List[1] in line and List[3].strip() == "g" :
                                        line = line.replace(List[1],List[2],-1)
                                f_new.write(line)
                        os.system("/bin/mv "+para_argv[3]+".bak "+para_argv[3])
                        print ("命令执行成功!")
                    else :
                        print ("无法识别的字符{}".format(List[0].strip()))
                else :
                    if List[0].strip()[:-1].isdigit() :
                        num = int(List[0].strip()[:-1])
                        with open(para_argv[3],"r") as f , open(para_argv[3]+".bak","w") as f_new :
                            for index,line in enumerate(f) :
                                if index+1 == num :
                                    if List[1] in line and List[3].strip() == "" :
                                        line = line.replace(List[1],List[2],1)
                                    elif List[1] in line and List[3].strip() == "g" :
                                        line = line.replace(List[1],List[2],-1)
                                f_new.write(line)
                        os.system("/bin/mv "+para_argv[3]+".bak "+para_argv[3])
                        print ("命令执行成功!")
                    else :
                        print ("{}不是一组数字,无法识别".format(List[0].strip()[:-1]))
        elif os.path.isfile(para_argv[3]) and os.access(para_argv[3],os.R_OK) and os.access(para_argv[3],os.W_OK):
            print ("为了节约时间,只对用户是root的情况做出判断。")
        else :
            print ("error!{}不是一个有效的文件或者文件的读写权限有问题。".format(para_argv[3]))
    else :
        print ("error! 格式为:{} -i '{过滤条件}' filename".format(para_argv[0]))
elif type(para_argv[1]) == str and len(para_argv) == 3 :
    print ("为了节约时间,只实现了有参数-i的对文件的替换操作")
else :
    print ("您输入的参数有误!") 
    print ("格式:{} [-i] '过滤条件' filename".format(para_argv[0]))
#!/usr/bin/env python3
# -*- coding:ut



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: