Home > Article > Backend Development > Introduction to commonly used modules in Python
In programming, a program or subroutine required to complete a certain function; or it refers to an independent program unit that can be processed by a compiler, assembly program, etc.; or it refers to a part of a large software system. This article introduces you to two commonly used modules in Python.
os:
This module contains common operating system functions. This module can be used to write platform-independent programs. For example, os.sep can be used to replace operating system-specific path separators.
List some commonly used methods in the os module:
os.name: Get the current system platform, return 'nt' under Windows, and 'posix' under Linux.
os.linesep: Get the line terminator used by the current platform. Returns '/r/n' under Windows and '/n' for Linux.
os.getcwd(): Get the current working directory, which is the directory path where the current python script works.
os.listdir(path): Returns all file and directory names in the specified directory.
For example:
Python code
>>> os.listdir('/home/shirley/')
The os.remove(path/filename) function is used to delete a file.
The os.system() function is used to run shell commands. This command can conveniently call or execute other scripts and commands
For example:
Python code
#打开记事本 >>>os.system('notepad') #打开指定的文件 >>>os.system('notepad shirley_python.txt')
The os.path.split() function returns the directory name and file name of a path.
For example:
Python code
>>> os.path.split('/home/shirley/myself/code/icbc.txt') ('/home/shirley/myself/code', 'icbc.txt')
The os.path.isfile() and os.path.isdir() functions check whether the given path is a file or a directory respectively.
Similarly, the os.path.existe() function is used to check whether the given path actually exists.
sys:
The sys module has many functions. You can refer to the python documentation http://docs.python.org/library/sys.html.
List how to use some commonly used functions:
sys.argv: implements passing parameters from outside the program to the program.
For example:
The content of the print.py script is:
Python code
import sys print sys.argv[0] print sys.argv[1] print sys.argv[2]
Execute in the interpreter:
Python code
>>>python print.py arg1 arg2
Generally speaking, argv[0] represents the file name of the executed program, that is, print.py, argv[1], and argv[2] respectively correspond to arg1 and arg2 in the interpreter command.
sys.exit([arg]): Exit in the middle of the program, arg=0 means normal exit.
sys.getdefaultencoding(): Gets the current encoding of the system, which generally defaults to ascii.
sys.setdefaultencoding(): Set the system default encoding. You will not see this method when executing dir (sys). If the execution fails in the interpreter, you can execute reload(sys) first, and then execute setdefaultencoding('utf8'). This method Set the system default encoding to utf8. (See Setting System Default Encoding)
sys.getfilesystemencoding(): Gets the encoding used by the file system. It returns 'mbcs' under Windows and 'utf-8' under mac.
sys.path: Gets the string collection of the specified module search path. You can put the written module under a certain path, and you can find it correctly when importing in the program.
sys.platform: Get the current system platform.
The above is the detailed content of Introduction to commonly used modules in Python. For more information, please follow other related articles on the PHP Chinese website!