######### using and Importing modules ####################
What is a module?
Third-party, others or themselves have developed a module, between different programs can be loaded into the import, call inside the function.
OS: Modules interacting with the operating system
>>> Import OS
>>> os.system (' df-th ')
Filesystem Type Size used Avail use% mounted on
/dev/xvda2 ext4 26G 7.1G 17G 30%/
Tmpfs tmpfs 919M 0 919M 0%/dev/shm
/DEV/XVDA1 ext4 291M 57M 219M 21%/boot
0----Executes the state returned by the command, which can be stored in a variable.
>>> cur_dir=os.system (' pwd ')
/home/robin
>>> print Cur_dir #存放的是命令返回状态, not output from command execution
0
The method of storing the result of the command in a variable:
Method One:
>>> Import OS
>>> os.popen (' pwd ')
<open file ' pwd ', mode ' R ' at 0x7f311fd6ec00>
>>> a=os.popen (' pwd '). Read ()
>>> A
'/home/robin\n '
Method Two:
>>> Import Commands
>>> result=commands.getstatusoutput (' pwd ')
>>> result
(0, '/home/robin ') # #同时存放了命令的返回状态和命令执行的输出结果
Method Three:
>>> Import subprocess
Use the tab completion feature in Python
>>> Import Tab
You can then use the tab completion feature.
Add:
1,os.walk Traversing a directory tree
eg
>>> a=os.walk ('/tmp ')
>>> a.next ()
('/tmp ', ['. Ice-unix ', ' ssh-tfigem3099 ', ' gitosis '], [' Zabbix_agentd.pid ', ' logrotate.pid ', ' zabbix_agentd.log ', ' Php-cgi.sock ', ' Supervisor.sock ', ' mounts.tmp '])
>>> A.next ()
('/tmp/. Ice-unix ', [], [])
2,sys Module
[email protected] robin]# cat ex1.py
#!/usr/bin/env python
# Coding:utf-8
Import Sys
Print sys.argv #打印执行脚本后面跟的参数
Print sys.argv[0] #打印执行脚本后面跟的指定参数
Print Sys.argv[1]
Print Sys.argv[2]
[[email protected] robin]# python ex1.py arg1 A 3 4 5
[' ex1.py ', ' arg1 ', ' A ', ' 3 ', ' 4 ', ' 5 ']
ex1.py
Arg1
A
How to import Modules
Import Sys
Import all the contents of the Sys module, and then call the method through the form SYS.ARGV
From sys import ARGV
Specifies the argv method under the Import SYS module, and then calls directly through the form argv
eg
[email protected] robin]# cat ex1.py
#!/usr/bin/env python
# Coding:utf-8
From sys import ARGV
Print argv
Print Argv[0]
Print Argv[1]
Print Argv[2]
[[email protected] robin]# python ex1.py arg1 A 3 4 5
[' ex1.py ', ' arg1 ', ' A ', ' 3 ', ' 4 ', ' 5 ']
ex1.py
Arg1
A
Similarly, if a from sys import * is written, any method under the calling module can be used argv to write the method name directly, Instead of SYS.ARGV---this is not recommended because, for example, there is a path method in the Sys module, and if the path variable is also defined in the program, then the call is overwritten by a conflict. There are many methods in calling the Sys module that do not know the name, so there may be conflicts.
#给模块起别名
Import multiprocessing as Multi
######### user interaction and formatted output ########################
######### Process Control if for loop #######################
######### while loop-based interrupt control ########################
######### Job Requirements ########################
First day 11-15 Python basics