使用 sys.argv 获取命令行参数
argv 列表保存了所有的命令行参数。列表中第一个元素是命令行程序的名称,其余的命令行参数以字符串的形式保存在该列表中。
# test_argv.py
import sys
print(sys.argv)
python sys_argv.py
['test_argv.py']
python test_argv.py localhost 3306
['test_argv.py', 'localhost', '3306']
# test_argv2.py
import os
import sys
def 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()
``````bash
python test_argv2.py test_argv.py
test_argv.py is accessible
使用 sys.stdin 和 fileinput 读取标准输入
# read_stdin.py
import sys
for line in sys.stdin:
print(line, end="")
cat test_argv.py | python read_stdin.py
import sys
print(sys.argv)
python read_stdin.py < test_argv.py
import sys
print(sys.argv)
python read_stdin.py -
test
test
python
python
# read_stdin2.py
import sys
def 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.py
import fileinput
for line in fileinput.input():
print(line, end="")
❯ cat read_stdin2.py | python read_from_fileinput.py
import sys
def get_content():
return sys.stdin.readlines()
print(get_content())
❯ python read_from_fileinput.py < read_stdin2.py
import sys
def get_content():
return sys.stdin.readlines()
print(get_content())
❯ python read_from_fileinput.py read_stdin.py read_stdin2.py
import sys
for line in sys.stdin:
print(line, end="")
import sys
def get_content():
return sys.stdin.readlines()
print(get_content())
# read_from_fileinput2.py
import fileinput
for 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.py
read_stdin.py 3 1 True False import sys
read_stdin.py 3 2 False False
read_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.py
import sys
sys.stdout.write('hello')
sys.stderr.write('world')
❯ python test_stdout_stderr.py
worldhello
❯ python test_stdout_stderr.py >/dev/null
world
❯ python test_stdout_stderr.py 2>/dev/null
hello
# test_stdout_stderr2.py
import sys
sys.stderr.write('error message')
sys.exit(1)
❯ python test_stdout_stderr2.py
error message
❯ echo $?
1
使用 getpass 库读取密码
getpass 与 input 区别在于,它不会将我们输入的密码显示在命令行中
# test_getpass.py
import getpass
user = getpass.getuser()
passwd = getpass.getpass('your password: ')
print(user, passwd)
❯ python test_getpass.py
your password:
kchou test