使用 sys.argv 获取命令行参数
argv 列表保存了所有的命令行参数。列表中第一个元素是命令行程序的名称,其余的命令行参数以字符串的形式保存在该列表中。
# test_argv.pyimport sysprint(sys.argv)
python sys_argv.py['test_argv.py']python test_argv.py localhost 3306['test_argv.py', 'localhost', '3306']
# test_argv2.pyimport osimport sysdef main():sys.argv.append("") # 防止索引越界filename = sys.argv[1]if not os.path.isfile(filename):raise SystemExit(filename, ' does not exists')# Use the real uid/gid to test for access to a path.elif not os.access(filename, os.R_OK):raise SystemExit(filename, ' is not accessible')else:print(filename, ' is accessible')if __name__ == '__main__':main()``````bashpython test_argv2.py test_argv.pytest_argv.py is accessible
使用 sys.stdin 和 fileinput 读取标准输入
# read_stdin.pyimport sysfor line in sys.stdin:print(line, end="")
cat test_argv.py | python read_stdin.pyimport sysprint(sys.argv)python read_stdin.py < test_argv.pyimport sysprint(sys.argv)python read_stdin.py -testtestpythonpython
# read_stdin2.pyimport sysdef get_content():return sys.stdin.readlines()print(get_content())
❯ python read_stdin2.py < read_stdin.py['import sys\n', '\n', 'for line in sys.stdin:\n', ' print(line, end="")\n']❯ python read_stdin2.py < test_argv.py['import sys\n', '\n', 'print(sys.argv)\n']
fileinput
依次读取命令行参数中给出的文件。也就是说,fileinput 会遍历 sys.argv[1:] 列表,并按行依次读取列表中的文件。如果列表为空,则 fileinput 默认读取标准输入中的内容。
# read_from_fileinput.pyimport fileinputfor line in fileinput.input():print(line, end="")
❯ cat read_stdin2.py | python read_from_fileinput.pyimport sysdef get_content():return sys.stdin.readlines()print(get_content())❯ python read_from_fileinput.py < read_stdin2.pyimport sysdef get_content():return sys.stdin.readlines()print(get_content())❯ python read_from_fileinput.py read_stdin.py read_stdin2.pyimport sysfor line in sys.stdin:print(line, end="")import sysdef get_content():return sys.stdin.readlines()print(get_content())
# read_from_fileinput2.pyimport fileinputfor line in fileinput.input():meta = [fileinput.filename(), fileinput.fileno(), fileinput.filelineno(),fileinput.isfirstline(), fileinput.isstdin()]print(*meta, end="\t")print(line, end="")
❯ python read_from_fileinput2.py read_stdin.pyread_stdin.py 3 1 True False import sysread_stdin.py 3 2 False Falseread_stdin.py 3 3 False False for line in sys.stdin:read_stdin.py 3 4 False False print(line, end="")❯ python read_from_fileinput2.py < read_stdin.py<stdin> 0 1 True True import sys<stdin> 0 2 False True<stdin> 0 3 False True for line in sys.stdin:<stdin> 0 4 False True print(line, end="")
使用 SystemExit 异常打印错误信息
# test_stdout_stderr.pyimport syssys.stdout.write('hello')sys.stderr.write('world')
❯ python test_stdout_stderr.pyworldhello❯ python test_stdout_stderr.py >/dev/nullworld❯ python test_stdout_stderr.py 2>/dev/nullhello
# test_stdout_stderr2.pyimport syssys.stderr.write('error message')sys.exit(1)
❯ python test_stdout_stderr2.pyerror message❯ echo $?1
使用 getpass 库读取密码
getpass 与 input 区别在于,它不会将我们输入的密码显示在命令行中
# test_getpass.pyimport getpassuser = getpass.getuser()passwd = getpass.getpass('your password: ')print(user, passwd)
❯ python test_getpass.pyyour password:kchou test
