One, OS module
An OS module is an interface that interacts with the operating system
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4 " "5 Os.walk () displays all files and subdirectories in the directory as Ganso, the first is the directory, the second is the folder, the third is the file6 Open (R ' Tmp\inner\file ', W) create file7 OS.GETCWD () Gets the current working directory, that is, the directory path of the current Python script work can record the current file directory first8 Os.chdir ("dirname") changes the current script working directory, equivalent to the shell CD9 Os.curdir Returns the current directory: ('. ') No use .Ten Os.pardir Gets the parent directory string name of the current directory: (' ... ') No use . One os.makedirs (' dirname1/dirname2 ') can generate a multi-level recursive directory dirname1 if the existence is created below, does not exist all created, if all exist on the error, can be A Modify the inside Exist_ok=ok to solve this error - os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on, - But if the directory is not empty, first you have to delete the file, or error the os.mkdir (' dirname ') generates a single-level directory, equivalent to mkdir dirname in the shell - os.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname - os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list - os.remove () delete a file + os.rename ("Oldname", "newname") renaming files/directories - os.stat (' path/filename ') get File/directory information + OS.SEP output operating system-specific path delimiter, win under "\", Linux for "/" A os.linesep Output The line terminator used by the current platform, win under "\t\n", Linux "\ n" at os.pathsep output is used to split the file path of the string win down for;, Linux for: - The os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX ' - os.system ("Bash command") runs a shell command that directly displays - os.popen ("Bash command") run shell command to get execution results - print (Ret.read ()) to read the results of Popen - Os.environ getting system environment variables in - Os.path pathn in parentheses are folders and files to Os.path.abspath (path) returns the absolute path normalized by path + os.path.split (path) splits path into directory and file name two tuples returned - os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path) the os.path.basename (path) returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) * os.path.exists (path) returns true if path exists, false if path does not exist $ os.path.isabs (path) returns True if path is an absolute pathPanax Notoginseng os.path.isfile (path) returns True if path is a file that exists. Otherwise returns false - os.path.isdir (path) returns True if path is a directory that exists. Otherwise returns false the Os.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored + Print (Os.path.join (OS.GETCWD (), ' filename ')) A os.path.getatime (path) returns the last access time of the file or directory to which path is pointing the os.path.getmtime (path) returns the last modified time of the file or directory to which path is pointing + os.path.getsize (path) returns the size of path - " " $ #Note: os.stat (' Path/filename ') Gets the structure description of the file/directory information $ " " - STAT Structure: - st_mode:inode Protection Mode the The st_ino:inode node number. - the device that the St_dev:inode resides on. Wuyi St_nlink:inode number of links. the St_uid: The owner's user ID. - St_gid: The group ID of the owner. Wu st_size: The size of the normal file in bytes, including data waiting for certain special files. - St_atime: Time of last visit. About St_mtime: The time of the last modification. $ st_ctime: "CTime" reported by the operating system. On some systems, such as UNIX, is the time of the most recent metadata change, and on other systems (such as Windows) is the time of creation (see the documentation for the platform for more information). - " "
Two, sys module
SYS module is an interface that interacts with the Python interpreter
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4SYS.ARGV command line argument list, the first element is the path of the program itself#do permission to use5Sys.exit (n) exit program, Exit normally (0)#Common6 Sys.version get version information for Python interpreter7 Sys.maxint the largest int value8 Sys.path Returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing9 Sys.platform returns the operating system platform nameTen " " One # import SYS A # # >python "2os&sys.py" Egon somebody #程序执行方式 - # Print (sys.argv) #打印参数, 0 is "2os&sys.py" program file name 1 is Egon 2 is somebody - # name = sys.argv[1] the # password = sys.argv[2] - # if Name = = ' Egon ' and password = = ' Somebody ': - # Print (' Continue to execute program ') - # Else: + # sys.exit (' Login failed ')
Python Development Module Basics: Os&sys