First, the basic concept:
1. Module
There are two types of modules in Python, one is a standard module, Python comes with a module, and the other is a third-party module that needs to be downloaded via Easy_install or PIP.
The first week of learning the Getpass module is the standard module, through the import getpass can be called
Two modules commonly used in System management, OS and SYS modules
1) OS module:
Os.system:
>>>ImportOS>>> Os.system ('df-h') Filesystem Size Used Avail capacity iused ifree%iused mounted on/dev/disk1 233Gi 201G I 31Gi 87% 1734983 4293232296 0%/Devfs 328Ki 328Ki 0Bi100% 1136 0 100%/Devmap-hosts 0Bi 0Bi 0Bi 100% 0 0 100%/Netmap auto_home 0B I 0Bi 0Bi100% 0 0 100%/Home0
Os.system command is a one-time command, the command output on the screen, no saved values, to do the experiment comparison:
>>> cmd = Os.system ('df-h') Filesystem Size Used Avail capacity iused ifree%iused mounted on/dev/disk1 233Gi 201G I 31Gi 87% 1734983 4293232296 0%/Devfs 328Ki 328Ki 0Bi100% 1136 0 100%/Devmap-hosts 0Bi 0Bi 0Bi 100% 0 0 100%/Netmap auto_home 0B I 0Bi 0Bi100% 0 0 100%/Home/DEV/DISK2S1 121Gi 74G I 46Gi 62% 609531 380413 62%/volumes/TK card>>>Print(cmd) 0
Print cmd only returns 0 because the command output is correct and returns 0
Os.popen ():
If you need to save the command output return value in memory, you need to use the Os.popen function to get the command return value by using the Read () method:
>>> cmd = os.popen ('df-h')print(cmd)' Df-h'r' at 0x10c9385d0>
>>> cmd = Os.popen ('df-h')>>>Print(CMD)<open file'df-h', mode'R'at 0x10c9385d0>>>> cmd = Os.popen ('df-h'). Read ()>>>Print(CMD) Filesystem Size used Avail capacity iused ifree%iused mounted on/dev/disk1 233Gi 201G I 31Gi 87% 1734987 4293232292 0%/Devfs 328Ki 328Ki 0Bi100% 1136 0 100%/Devmap-hosts 0Bi 0Bi 0Bi 100% 0 0 100%/Netmap auto_home 0B I 0Bi 0Bi100% 0 0 100%/home
Os.mkdir ():
Os.mkdir function to create a directory
>>> os.mkdir ('test')>>> os.system ('ls-l ' ) Applications Downloads Music pycharmprojects bcache test
2) SYS module:
Sys.path function:
The Sys.path function can print all the environment variables used by Python
sys.path['/library/python/2.7/site-packages/django-1.8.3-py2.7.egg' '/library/python/2.7/site-packages/pip-7.1.2-py2.7.egg',]
Python Learning Week Two