python读取文件参数_Python--解析命令参数和读取文件

# -*- coding: utf-8 -*-

# =============================================================================

# FileName: args_demo.py

# Desc: 演示解析命令参数和读取文件

# Author: GGA

# Email:

# HomePage:

# Version: 1.0.1

# LastChange: 2020-12-20

# History:

# =============================================================================

import sys

import argparse

import logging

logger = logging.getLogger()

def init_logger():

logger.setLevel(level=logging.DEBUG)

logger_format = logging.Formatter("[%(asctime)s]-[%(levelname)s]: %(message)s") # output format

sh = logging.StreamHandler(stream=sys.stdout) # output to standard output

sh.setFormatter(logger_format)

logger.addHandler(sh)

def get_parser():

parser = argparse.ArgumentParser(

description='Parse MySQL binlog to SQL you want',

add_help=False)

connect_setting = parser.add_argument_group('connect setting')

parser.add_argument(

'--help',

dest='help',

action='store_true',

help='help information',

default=False

)

connect_setting.add_argument(

'--host_file',

dest='host_file',

type=str,

default=None,

help='the file path which contain host info'

)

return parser

def parse_args(command_args):

need_print_help = False if command_args else True

parser = get_parser()

args = parser.parse_args(command_args)

if args.help or need_print_help:

parser.print_help()

sys.exit(1)

if args.host_file is None:

logger.warning("parameter host_file must input")

parser.print_help()

return args

def main(command_args):

args = parse_args(command_args)

logger.info(args.host_file)

file_handler = open(args.host_file, "r")

for line in file_handler.readlines():

logger.info(line)

if __name__ == '__main__':

init_logger()

main(sys.argv[1:])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值