os, sys module

os module

 Os module is an interface to interact with the operating system

os.makedirs ( 'dirname1 / dirname2') may generate a multilayer recursive directory 
os.removedirs ( 'dirname1') if the directory is empty, delete, and recursively to the parent directory, should be empty, delete, and so analogy 
os.mkdir ( 'dirname') to generate a single level directory; it is equivalent to the shell dirname mkdir 
os.rmdir ( 'dirname') empty directory delete a single stage, if the directory is not empty can not be deleted, being given; corresponds to the shell rmdir dirname 
os.listdir ( 'dirname') lists all the files and subdirectories in the specified directory, including hidden files, and print in a list 
os.remove () to delete a file 
os.rename ( "oldname", "newname ") weight name the file / directory 
os.stat ( 'path / filename') get the file / directory information 

os.system ( "bash command") to run shell commands, displayed directly 
os.popen ( "bash command) .read ( ) run shell commands, get the results from 
os.getcwd () Gets the current working directory, that directory path of the current python script work 
os.chdir ( "dirname") script to change the current working directory; cd at the equivalent of shell
os.path
os.path.abspath (path) Returns the path normalized absolute path 
os.path.split (path) into the path directory and file name tuple returned os.path.dirname (path) Returns the directory path of. In fact, os.path.split (path) of the first element os.path.basename (path) Returns the last path of the file name. How path to / \ or end, it will return a null value. I.e. The os.path.split (path) of the second element os.path.exists (path) path, if present, returns True; if the path does not exist, returns False os.path.isabs (path) if the path is an absolute path, returns True os.path.isfile (path) if the path is a file exists, returns True. Otherwise it returns False os.path.isdir (path) if the path is a directory exists, then return True. Otherwise it returns False os.path.join (path1 [, path2 [, ...]]) will return after a combination of multiple paths parameters before the first absolute path will be ignored os.path.getatime (path) return path points to the last access time of a file or directory os.path.getmtime (path) returns the file or directory path points to the last modification time os.path.getsize (path) return path size

 

Note: the os.stat ( 'path / filename' ) obtaining the file / directory information  of the structure description  

stat structure: 

The st_mode: the inode protection mode 
st_ino: inode node number. 
st_dev: inode device resides. 
st_nlink: inode number of links. 
st_uid: User ID of the owner. 
st_gid: group ID of the owner. 
st_size: common file unit size in bytes; wait for data containing special file. 
st_atime: Time last accessed. 
st_mtime: last modification time. 
st_ctime: report by the operating system's " ctime " . On some systems (such as Unix) is the time of most recent metadata changes, on the other systems (such as Windows) is the creation time (see platform documentation for more information).
stat structure
os.sep output operation system-specific path separator for the next win " \\ " , is the Linux " / " 
os.linesep output current platform using line terminator, to win the " \ R & lt \ n- " , under Linux as " \ n- " 
is the character string for dividing a win os.pathsep output file path;, under Linux is: 
the os.name output string indicating the current use internet. win -> ' NT ' ; Linux-> ' POSIX '
Attributes

 

sys module

sys module is an interactive interface python interpreter

sys.argv command line parameters List, the first element of the program itself is the path 
sys.exit (n) to exit the program, when the normal exit exit (0), exit the error sys.exit (1) 
sys.version get the version of Python interpreter information 
sys.path return module search path, use the PYTHONPATH environment variable initialization value 
sys.platform returns the name of the operating system platform
import sys
try:
    sys.exit(1)
except SystemExit as e:
    print(e)
import sys
try:
    sys.exit(1)
except SystemExit as e:
    print(e)

Guess you like

Origin www.cnblogs.com/shangping/p/11488048.html