#/usr/bin/python#coding =utf8# @Time: 2017/11/11 3:15# @Auther: Liuzhenchuan# @File: OS module. PYimport os #1. Get the operating system type via OS Os.name = Windows is NT type, Os.name = Linux is a POSIX typeprint(os.name) #2. Executing system commandsexecuted under #在windows cmd. The character format for Windows is gkm. Commonly used is the UTF8Print(os.system (' ipconfig ')) #3. Get the results of system commands under Windows with the Os.popen () module, the Os.popen () module returns the file format,# So you can read the execution results of the system commands through File.read () and find the 192.168.16.1 by using the Find () commandcontent = Os.popen (' ipconfig '). Read () print content.find (' 192.168.16.1 ') # 4. Methods of the OS modulePrint dir(OS) #5. Os.listdir lists the files under the directory. '. ' point is the current directory. print os.listdir ('. ') )# 76OS.GETCWD () returns the current working directoryprint os.getcwd () print Os.listdir (OS.GETCWD ()) # 7. Toggle the current directory,print os.chdir (R ' E: ') print os.getcwd () #8 create ' abc ' in the current directory)os.mkdir (' abc ')# 9 Delete the current directory fileos.remove (' 1.log ')# 10 Print operating system delimiter, Linux system is \ n; Windows system is \ r \ n; Mac system is \ rprint os.linesep ()# 11. Determine if the directory exists in the current directory, and if not, createprint ' # # ' *5 + " to determine whether the directory exists ' *5 If not os.path.exists (' Test '): os.mkdir (' Test ') else: Print ' test is ok ' # 12. Stitching Directoryprint ' # # ' *5 + " stitching directory ' + ' # # ' *5 print os.path.join ('. ') ,' aaa ',' BB ', 'cc ') # 13. Get the top level directory print ' # # ' * 5 + " obtains the previous level directory ' + ' # # ' *5 print os.path.dirname (R ' e:\ ape Lesson python script \modules ')
Python Base 7.4 os module