OS module is also our usual work in a very common to a module, through OS the module calls the system command, obtains the road strength, gets the type of the operating system, and so on is using the module.
1. Get the system type
Example:
>>> Import OS
>>> Print (os.name)
Posix
2. Execute system commands
Example 1:
>>> Import OS
>>> Os.system ("Ifconfig")
Ens33:flags=4163<up,broadcast,running,multicast> MTU 1500
inet 192.168.2.230 netmask 255.255.254.0 broadcast 192.168.3.255
Inet6 fe80::b8ce:3bee:4221:ea32 Prefixlen ScopeID 0x20<link>
Ether 00:0c:29:13:a3:a6 Txqueuelen (Ethernet)
RX packets 6804684 Bytes 830957408 (792.4 MiB)
RX Errors 0 dropped 222021 overruns 0 frame 0
TX packets 111583 Bytes 16210846 (15.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Example 2:
Popen () Returns a file object that obtains the final result by File.read ()
>>> content = Os.popen ("Ifconfig"). Read ()
>>> Print (content)
Ens33:flags=4163<up,broadcast,running,multicast> MTU 1500
inet 192.168.2.230 netmask 255.255.254.0 broadcast 192.168.3.255
Inet6 fe80::b8ce:3bee:4221:ea32 Prefixlen ScopeID 0x20<link>
Ether 00:0c:29:13:a3:a6 Txqueuelen (Ethernet)
RX packets 6813500 Bytes 832023527 (793.4 MiB)
RX Errors 0 dropped 222508 overruns 0 frame 0
TX packets 111846 Bytes 16233128 (15.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3. File and directory operations
3.1 current path:os.getcwd ()
Example:
>>> print (OS.GETCWD ())
/root
3.2 Switch Directories: os.chdir (' target directory ')
Example:
>>> Os.chdir ("/data/temp") # or Os.chdir (r "/root")
>>> OS.GETCWD ()
'/data/temp '
3.3 Listing Files: os.listdir (' string directory ')
Example:
>>> Os.listdir (OS.GETCWD ())
['. Bash_logout ', '. Bash_profile ', '. CSHRC ', '. TCSHRC ', ' anaconda-ks.cfg ', '. Bash_history ', '. BASHRC ', ' 4.sh ', ' 10.sh ', '. Jenkins ', '. Groovy ', '. Python-eggs ', '. Mongorc.js ', '. Dbshell ', '. Gitconfig ', '. Java ', '. Viminfo ', ' test.sh ', '. mysql _history ', '. PKI ', ' tip.sh ', '. Oracle_jre_usage ', '. Rnd ', '. bashrc_2017-08-21 ', ' 2.sh ', '. Cache ', '. config ', ' 6.txt ', ' Centos_hzp.tar ', ' Container.tar ', '. ssh ', '. bashrc_2017-08-18 ', ' 1.sh ', ' 5.sh ', '. NPM ']
>>> Os.listdir ("/data/temp")
[' Health_8102.log ', ' health_1-3.log ', ' projects1.txt ', ' create_branch_status ', ' Test ', ' create_branch.sh ', ' Api.txt.bak ', ' projects.txt.bak ', ' modify_configure ', ' test2 ']
3.4 Creating a directory: os.mkdir (' directory ')
Example
>>> Os.mkdir ("test20171023")
>>> Os.listdir (OS.GETCWD ())
[' Health_8102.log ', ' health_1-3.log ', ' projects1.txt ', ' create_branch_status ', ' Test ', ' create_branch.sh ', ' Api.txt.bak ', ' projects.txt.bak ', ' modify_configure ', ' test2 ', 'test20171023']
3.5 Delete files under directory os.remove (' filename ')
Example
>>> os.remove ("1.txt")
Description: No error is present
3.6 Printing system separators: Os.linesep
Example
Print (OS.LINESEP)
Description: Linux is \ n; Windows is \ r \ n; Mac is \ r
3.7 Displaying the file directory: os.path.dirname (R '/data/temp/1.txt ')
Example
>>> Print (Os.path.dirname (R '/data/temp/1.txt '))
/data/temp
3.8 Stitching Multilevel Directory: Os.path.join (OS.GETCWD (), ' AAA ', ' BBB ', ' CCC ')
Example
>>> Print (Os.path.join (OS.GETCWD (), ' abc.txt '))
/data/temp/abc.txt
Description: Does not create
3.9 File and path separation: os.path.split (path1)
Example
>>> path1 = Os.path.join (OS.GETCWD (), ' abc.txt ')
>>> Print (Os.path.split (path1))
('/data/temp ', ' abc.txt ')
4.0 path and extension separated:os.path.splitext (' file ')
Example
>>> path1 = Os.path.join (OS.GETCWD (), ' abc.txt ')
>>> Print (Os.path.splitext (path1))
('/data/temp/abc ', '. txt ')
4.1 file does not exist create:os.path.exists (' directory ')
Example
#! /usr/bin/python
Import OS
If not os.path.exists (R '/data/temp/test20171024 '):
Os.mkdir (R '/data/temp/test20171024 ')
4.2 Link:os.path.islink (path)
Example
>>> Print (Os.path.islink (OS.GETCWD ()))
False
Description: returns False if the system does not support links
More ways:
Print (dir (OS))
..............
Python module-os