1. associated with the command line Python language features
1) Using sys.argv get command line arguments
import sys print(sys.argv)
2). Sys.stdin using standard input and read fileinput
import sys def get_content(): return sys.stdin.readlines() print(get_content()) 使用Ctrl+d退出
fileinput is very simple to use, in most cases, we directly call fileinput module input method by reading the contents of the line can be used for loop through the contents of the file # CAT read_from_fileinput.py from __future__ Import print_function Import fileinput for Line in fileinput.input ( ): Print (Line, End = " " ) # CAT / etc / passwd | Python read_from_fileinput.py
3) Use SystemExit abnormal print an error message
4) Use the library to read the password getpass
import getpass user = getpass.getuser() passwd = getpass.getpass('your password: ') print(user,passwd)
2. Use configparser parsing the configuration file
import configparser cf = configparser.ConfigParser(allow_no_value=True) cf.read('/etc/my.cnf') print(cf.sections()) print(cf.has_section('client')) print(cf.options('client')) print(cf.has_option('client','user')) print(cf.get('client','user'))
3. Use argparse parse command line arguments
1) .ArgumentParse parser
2). Mimic the MySQL client's command line arguments
4. Use logging logs
Role 1). Log
2) .Python a logging module
3) Configure log format
The command line associated with open source projects
1) using the parse command line parameters click
2) Use prompt_toolkit create an interactive command-line tool