修改文件

def select_env(command):
    # 根据输入选择脚本运行环境
    loca_hosts = "/etc/hosts"
    new_hosts = "# test_env\n\
59.111.241.152 se.stu.126.net\n\
59.111.241.152 s.stu.126.net\n\
59.111.241.152 sa.stu.126.net\n\
59.111.241.152 sb.stu.126.net\n\
59.111.241.152 st.stu.126.net\n\
59.111.241.152 study.163.com\n\
59.111.241.152 fes1.study.163.com\n\
59.111.241.152 zhangbiying.study.163.com\n\
59.111.241.152 b.study.163.com\n\
59.111.241.152 admin.study.163.com\n\
59.111.241.152 kftest.study.163.com\n\
59.111.241.152 up.study.163.com\n\
59.111.241.152 yunqike.163.com\n\
59.111.241.152 sp.stu.126.net\n\
59.111.241.152 swl1.study.163.com\n\
59.111.241.152 admin.yunqike.163.com\n\
59.111.241.152 yytest164.study.163.com\n"

    if command == "1":
        new_fp = open (loca_hosts, "w")
        new_fp.truncate()
        new_fp.write(new_hosts)
        new_fp.close()
        return True
    elif command == "2":
        new_fp = open (loca_hosts, "w")
        # 清除文本内容,如果使用remove可能会权限不足
        new_fp.truncate ()
        new_fp.write ("# My hosts")
        new_fp.close ()
        return False
    else:
        return None

你可能感兴趣的:(修改文件)