Home > Article > Backend Development > Python learning - Python standard library
Python Standard Library
Python standard library is installed with Python, and it contains a large number of extremely useful modules. It is important to be familiar with the Python standard library because if you are familiar with the modules in these libraries, most of your problems can be solved quickly and easily using them.
You can learn the complete contents of all modules in the Python standard library in the "Library Reference" section of the documentation that comes with Python installation.
sys module
The sys module contains the corresponding functions of the system. We have already studied the sys.argv list, which contains command line arguments.
os module
This module contains common operating system functions. This module is especially important if you want your program to be platform-independent. That is, it allows a program to run under Linux and Windows without any changes or problems after being written. One example is using os.sep to replace operating system-specific path separators.
Listed below are some of the more useful parts in the os module. Most of them are simple and straightforward. A ● OS.Name string indicates the platform you are using. For example, for Windows, it is 'nt', and for Linux/Unix users, it is 'posix'.
● The os.getcwd() function gets the current working directory, which is the directory path where the current Python script works.
● The os.getenv() and os.putenv() functions are used to read and set environment variables respectively. I ● Os.listdir () Return all files and directory names in the specified directory. M ● Os.remove () function is used to delete a file.
os.system() function is used to run shell commands. In ● OS.LineSep string gives the line termination symbol used by the current platform. For example, Windows uses 'rn', Linux uses 'n' and Mac uses 'r'. At ● OS.Path.split () function returns a directory name and file name of a path. At ● OS.Path.isfile () and Os.Path.isdir () functions are tested to whether the path given by testing is a file or a directory. Similarly, the os.path.exists() function is used to check whether the given path actually exists.
You can use the Python standard documentation to explore more detailed knowledge about these functions and variables. You can also use help(sys) etc.