创建交互命令行
命令行交互工具
使用prompt_toolkit模块,使用前先安装
pip install prompt_toolkit
参考资料:官方文档
简易的交互命令行脚本
#python简易交互命令行脚本
# while True:
# user_input = input('>')
# print(user_input)
from prompt_toolkit import prompt
while True:
user_input = prompt('>')
print(user_input)
#对比python简易交互命令行脚本,prompt_toolkit支持ctrl+a跳到输入开头,ctrl+e跳到输入位置,\
#ctrl+k删除光标到末尾的内容。
添加历史记录功能
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
while True:
#会在当前目录下生成history.txt文件保存历史记录
user_input = prompt('>',history=FileHistory('history.txt'))
print(user_input)
#使用方向键查看历史记录
添加按→方向键补全历史记录
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
while True:
user_input = prompt('>',
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),)
print(user_input)
#prompt_toolkit会以暗色字体显示匹配到的历史输入,方向键→可以补全
添加按tab键补全
注意WordCompleter
移动到了prompt_toolkit.completion.word_completer
中
参考资料:WordCompleter文档
from prompt_toolkit import prompt
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.history import FileHistory
from prompt_toolkit.completion.word_completer import WordCompleter
SQLCompleter = WordCompleter(['select','from','insert','update','delete',
'drop'],ignore_case=True)
while 1:
user_input = prompt('SQL>',
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=SQLCompleter,)
print(user_input)
效果
读取标准输入
在python的sys库中有三个文件描述符
stdin: 标准输入
stdout: 标准输出
stderr: 错误输出
案例
#代码如下
import sys
for line in sys.stdin:
print(line, end ='')
#从标准输入中获取数据并打印
$cat /etc/passwd | python read_stdin.py
$python read_stdin.py < /etc/passwd
$python read_stdin.py - #从键盘输入
sys.stdin调用文件对象
import sys
def get_content():
return sys.stdin.readlines() #返回值是一个列表
print(get_content())
使用fileinput模块进行多文件处理,
import fileinput
for line in fileinput.input():
print(line,end='')
#从标准输入以及文件中获取数据并打印
$cat /etc/passwd | python read_from_fileinput.py
$python read_from_fileinput.py < /etc/passwd
$python read_from_fileinput.py /etc/passwd /etc/hosts
#跟stdin不同之处可以直接从文件中获取数据,而不需要管道符,重定向。
fileinput的常用方法
方法名 | 描述 |
---|---|
filename | 当前正在读取的文件名 |
fileno | 文件的描述符 |
filelineno | 显示正在读取文件的行是第几行 |
isfirstline | 正在读取的行是否是文件的第一行 |
isstdin | 判断从文件读取还是从标准输入读取,从文件读取为False |
案例 |
import fileinput
for line in fileinput.input():
meta =[fileinput.filename(),fileinput.fileno(),fileinput.filelineno(),
fileinput.isfirstline(),fileinput.isstdin(),'']
print(*meta,end='') #meta加*,会读取列表中的值
print(line,end="") #打印文件内容
'''
out:
$python fileinput_method.py /etc/passwd #*meta显示为
/etc/passwd 3 1 True Falseroot:x:0:0:root:/root:/bin/bash
/etc/passwd 3 2 False Falsebin:x:1:1:bin:/bin:/sbin/nologin
$python fileinput_method.py /etc/passwd #meta显示为
['/etc/passwd', 3, 1, True, False, '']root:x:0:0:root:/root:/bin/bash
['/etc/passwd', 3, 2, False, False, '']bin:x:1:1:bin:/bin:/sbin/nologin
'''
打印错误信息
使用sys模块的stdout以及stderr实现
sys.stdout: 从标准输出中输出内容
sys.stderr: 从错误输出中输出内容
案例
import sys
sys.stdout.write('hello') #很少用,print()函数默认会输出到终端
sys.stderr.write('world') #常用
'''
out:
$python test_stdout_stderr.py >/dev/null #标准输出导入黑洞文件,会显示错误输出
world
$python test_stdout_stderr.py 2>/dev/null
hello
'''
python程序执行失败,可以在标准错误中输出错误信息,返回非0返回码。
import sys
sys.stderr.write('error message')
sys.exit(22)
'''
out:
$python test_system_exit.py
error message
$echo $?
22
'''
读取密码
使用getpass模块中的getuser()跟getpass()
getpass()不会将密码显示在命令行中
import getpass
user = getpass.getuser() #获取的是登录用户名
passwd = getpass.getpass('your password: ')
print('your name is %s , your password is %s' %(user,passwd))
'''
out:
$python getpass.py
your password:
your name is root , your password is 123
'''
学习来自:《python linux系统管理与自动化运维》 第三章 ,命令行功能参考资料,命令行功能参考资料2
今天的学习是为了以后的工作更加的轻松!