Python modules os and sys

 The os module is an interface for interacting with the operating system

os.getcwd() Get the current working directory, that is, the directory path where the current python script works
os.chdir("dirname") Change the working directory of the current script; equivalent to cd under the shell
os.curdir returns the current directory: ('.')
os.pardir Get the parent directory string name of the current directory: ('..')
os.makedirs('dirname1/dirname2') can generate multi-level recursive directories
os.removedirs('dirname1') If the directory is empty, delete it and recurse to the previous directory, if it is also empty, delete it, and so on
os.mkdir('dirname') generates a single-level directory; equivalent to mkdir dirname in the shell
os.rmdir('dirname') deletes a single-level empty directory. If the directory is not empty, it cannot be deleted, and an error is reported; it is equivalent to rmdir dirname in the shell
os.listdir('dirname') List all files and subdirectories in the specified directory, including hidden files, and print them in a list
os.remove() removes a file
os.rename("oldname","newname") rename file/directory
os.stat('path/filename') Get file/directory information
os.sep output the OS-specific path separator, "\\" under win, "/" under Linux
os.linesep output the line terminator used by the current platform, "\t\n" under win, "\n" under Linux
os.pathsep outputs the string used to split the file path; under win, it is:
The os.name output string indicates the currently used platform. win->'nt'; Linux->'posix'
os.system("bash command") run the shell command and display it directly
os.popen("bash command).read() Run the shell command and get the execution result
os.environ Get system environment variables
os.path
os.path.abspath(path) returns the normalized absolute path of path
os.path.exists(path) Returns True if path exists; returns False if path does not exist
os.path.isabs(path) returns True if path is an absolute path
os.path.isfile(path) Returns True if path is an existing file. Otherwise return False
os.path.isdir(path) Returns True if path is an existing directory. Otherwise return False
os.path.join(path1[, path2[, ...]]) Returns after combining multiple paths, parameters before the first absolute path will be ignored
os.path.getatime(path) Returns the last access time of the file or directory pointed to by path
os.path.getmtime(path) Returns the last modification time of the file or directory pointed to by path
os.path.getsize(path) returns the size of path

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

 

stat structure:

st_mode: inode protection mode
st_ino: inode node number.
st_dev: The device where the inode resides.
st_nlink: The number of links for the inode.
st_uid: User ID of the owner.
st_gid: Group ID of the owner.
st_size: The size in bytes of a normal file; contains data awaiting some special files.
st_atime: The time of the last visit.
st_mtime: The time of the last modification.
st_ctime: "ctime" as reported by the operating system. On some systems (like Unix) the time of the last metadata change, on others (like Windows) the creation time (see the platform's documentation for details).

The sys module is an interface for interacting with the python interpreter

 

sys.argv command line parameter list, the first element is the path of the program itself
sys.exit(n) Exit the program, exit(0) when exiting normally, exit sys.exit(1) with error
sys.version Get the version information of the Python interpreter
sys.path returns the search path of the module, using the value of the PYTHONPATH environment variable during initialization
sys.platform returns the OS platform name
import sys
try:
    sys.exit(1)
except SystemExit as e:
    print (s)
Exception handling and status

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324966421&siteId=291194637