使用 sys.argv 获取命令行参数

argv 列表保存了所有的命令行参数。列表中第一个元素是命令行程序的名称,其余的命令行参数以字符串的形式保存在该列表中。

  1. # test_argv.py
  2. import sys
  3. print(sys.argv)
  1. python sys_argv.py
  2. ['test_argv.py']
  3. python test_argv.py localhost 3306
  4. ['test_argv.py', 'localhost', '3306']
  1. # test_argv2.py
  2. import os
  3. import sys
  4. def main():
  5. sys.argv.append("") # 防止索引越界
  6. filename = sys.argv[1]
  7. if not os.path.isfile(filename):
  8. raise SystemExit(filename, ' does not exists')
  9. # Use the real uid/gid to test for access to a path.
  10. elif not os.access(filename, os.R_OK):
  11. raise SystemExit(filename, ' is not accessible')
  12. else:
  13. print(filename, ' is accessible')
  14. if __name__ == '__main__':
  15. main()
  16. ``````bash
  17. python test_argv2.py test_argv.py
  18. test_argv.py is accessible

使用 sys.stdin 和 fileinput 读取标准输入

  1. # read_stdin.py
  2. import sys
  3. for line in sys.stdin:
  4. print(line, end="")
  1. cat test_argv.py | python read_stdin.py
  2. import sys
  3. print(sys.argv)
  4. python read_stdin.py < test_argv.py
  5. import sys
  6. print(sys.argv)
  7. python read_stdin.py -
  8. test
  9. test
  10. python
  11. python
  1. # read_stdin2.py
  2. import sys
  3. def get_content():
  4. return sys.stdin.readlines()
  5. print(get_content())
  1. python read_stdin2.py < read_stdin.py
  2. ['import sys\n', '\n', 'for line in sys.stdin:\n', ' print(line, end="")\n']
  3. python read_stdin2.py < test_argv.py
  4. ['import sys\n', '\n', 'print(sys.argv)\n']

fileinput

依次读取命令行参数中给出的文件。也就是说,fileinput 会遍历 sys.argv[1:] 列表,并按行依次读取列表中的文件。如果列表为空,则 fileinput 默认读取标准输入中的内容。

  1. # read_from_fileinput.py
  2. import fileinput
  3. for line in fileinput.input():
  4. print(line, end="")
  1. cat read_stdin2.py | python read_from_fileinput.py
  2. import sys
  3. def get_content():
  4. return sys.stdin.readlines()
  5. print(get_content())
  6. python read_from_fileinput.py < read_stdin2.py
  7. import sys
  8. def get_content():
  9. return sys.stdin.readlines()
  10. print(get_content())
  11. python read_from_fileinput.py read_stdin.py read_stdin2.py
  12. import sys
  13. for line in sys.stdin:
  14. print(line, end="")
  15. import sys
  16. def get_content():
  17. return sys.stdin.readlines()
  18. print(get_content())
  1. # read_from_fileinput2.py
  2. import fileinput
  3. for line in fileinput.input():
  4. meta = [fileinput.filename(), fileinput.fileno(), fileinput.filelineno(),
  5. fileinput.isfirstline(), fileinput.isstdin()]
  6. print(*meta, end="\t")
  7. print(line, end="")
  1. python read_from_fileinput2.py read_stdin.py
  2. read_stdin.py 3 1 True False import sys
  3. read_stdin.py 3 2 False False
  4. read_stdin.py 3 3 False False for line in sys.stdin:
  5. read_stdin.py 3 4 False False print(line, end="")
  6. python read_from_fileinput2.py < read_stdin.py
  7. <stdin> 0 1 True True import sys
  8. <stdin> 0 2 False True
  9. <stdin> 0 3 False True for line in sys.stdin:
  10. <stdin> 0 4 False True print(line, end="")

使用 SystemExit 异常打印错误信息

  1. # test_stdout_stderr.py
  2. import sys
  3. sys.stdout.write('hello')
  4. sys.stderr.write('world')
  1. python test_stdout_stderr.py
  2. worldhello
  3. python test_stdout_stderr.py >/dev/null
  4. world
  5. python test_stdout_stderr.py 2>/dev/null
  6. hello
  1. # test_stdout_stderr2.py
  2. import sys
  3. sys.stderr.write('error message')
  4. sys.exit(1)
  1. python test_stdout_stderr2.py
  2. error message
  3. echo $?
  4. 1

使用 getpass 库读取密码

getpass 与 input 区别在于,它不会将我们输入的密码显示在命令行中

  1. # test_getpass.py
  2. import getpass
  3. user = getpass.getuser()
  4. passwd = getpass.getpass('your password: ')
  5. print(user, passwd)
  1. python test_getpass.py
  2. your password:
  3. kchou test