I. OS module overview The Python OS module contains common operating system features. This module is especially important if you want your program to be platform-agnostic. (in one language) two, common Method 1, os.name output string indicates the platform being used. If the window is ' NT ', it is ' POSIX ' for Linux/unix users. 2. The OS.GETCWD () function gets the current working directory, which is the directory path of the current Python script work. 3, Os.listdir () returns all file and directory names under the specified directory. >>> Os.listdir (OS.GETCWD ()) [' Django ', ' DLLs ', ' Doc ', ' include ', ' Lib ', ' Libs ', ' LICENSE.txt ', ' Mysql-python-wininst.log ', ' NEWS.txt ', ' pil-wininst.log ', ' python.exe ', ' pythonw.exe ', ' README.txt ', ' Removemysql-python.exe ', ' RemovePIL.exe ', ' Removesetuptools.exe ', ' Scripts ', ' setuptools-wininst.log ', ' tcl ', ' Tools ', ' W9xpopen.exe ']>>> 4, os.remove () delete a file. 5, Os.system () run the shell command. >>> os.system (' dir ') 0>>> os.system (' cmd ') #启动dos6, OS.SEP can replace operating system-specific path separators. 7. Os.linesep string gives the line terminator used by the current platform >>> os.linesep ' \ r \ n ' #Windows使用 ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '. >>> os.sep ' \ \ ' #Windows >>> 8, Os.path.split () function returns the directory name and file name of a path >>> os.path.split (' c:\\ Python25\\abc.txt ') (' C:\\python25 ', ' abc.txt ') 9, Os.path.isfile () and Os.path.isdir () functions respectively examine the given pathThe path is a file or a directory. >>> Os.path.isdir (OS.GETCWD ()) true>>> os.path.isfile (' a.txt ') False10, os.path.exists () The function is used to verify that the given path really exists >>> os.path.exists (' c:\\python25\\abc.txt ') false>>> os.path.exists (' c:\\ Python25 ') true>>> 11, Os.path.abspath (name): Obtains absolute path 12, Os.path.normpath (PATH): Canonical path string form 13, Os.path.getsize (name): Get file size, if name is directory return 0L14, Os.path.splitext (): Detach file name with extension >>> os.path.splitext (' a.txt ') (' A ', '. txt ') 15, Os.path.join (path,name): Connection directory with file name or directory >>> os.path.join (' C:\\python ', ' a.txt ') ' c:\\python\\ A.txt ' >>> os.path.join (' C:\\python ', ' F1 ') ' c:\\python\\f1 ' >>> 16, Os.path.basename (PATH): Return file name >>> os.path.basename (' a.txt ') ' A.txt ' >>> os.path.basename (' c:\\python\\a.txt ') ' A.txt ' >> > 17, Os.path.dirname (PATH): Return file path >>> os.path.dirname (' c:\\python\\a.txt ') ' C:\\python '
Python OS module learning