python常用模块初始
1.getpass(密文输入)
1
2
3
4
|
import
getpass
#导入getpass模块,用于密文输入
name = input(
"input your name:"
)
passwd
= getpass.getpass(
"input your passwd:"
)
#密文输入
print (name,
passwd
)
|
2.OS模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/bin/bash/env python
#_*_ coding:utf-8 _*_
import
os
例
1
#直接执行linux命令“df -lh”
os.system(
"df -lh"
)
例
2
#创建abc目录
os.mkdir(
"abc"
)
例
3
#打印命令执行结果是否成功,成功返回None;
a
=
os.system(
"df -lh"
)
print
(a)
例
4
#将linux执行查询到的内容复制给变量
aa
=
os.popen(
"df -lh"
).read()
#先用popen读取df-lh 获取的内容读到内存,再用read读出来赋值给aa ;
print
(aa)
|
3.tab模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
查找python默认全局环境变量位置:
import
sys
#导入sys模块
print
(sys.path)
#打印python的所有目录,python放模块的目录是/usr/lib/python2.7/dist-packages
#把文件上传到该目录下,名字tab.py
import
tab
#os.就可以tab补全了;注意不能加.py
#tab模块要自己写;
#每一个脚本都可以是一个模块;
具体TAB模块内容:
#!/bin/bash/env python
#_*_ coding:utf-8 _*_
# python startup file
import
sys
import
readline
import
rlcompleter
import
atexit
import
os
# tab completion
readline.parse_and_bind(
'tab: complete'
)
# history file
histfile
=
os.path.join(os.environ[
'HOME'
],
'.pythonhistory'
)
try
:
readline.read_history_file(histfile)
except
IOError:
pass
atexit.register(readline.write_history_file, histfile)
del
os, histfile, readline, rlcompleter
|
4.sys模块
1
2
3
4
|
python查找全局环境变量路径 方法:
import
sys
#导入sys模块
print
(sys.path)
|
本文转自506554897 51CTO博客,原文链接:http://blog.51cto.com/506554897/1906413
,如需转载请自行联系原作者