Built-in modules (which can be used directly without import) Common built-in functions:
Python code
- Help (OBJ) online help. obj is of any type.
- Callable (OBJ) to check whether an OBJ can be called like a function
- Repr (OBJ) to get the OBJ representation string. You can use this string eval to recreate a copy of this object.
- Eval_r (STR) indicates a valid Python expression. This expression is returned.
- Dir (OBJ) view the name visible in the name space of OBJ
- Hasattr (OBJ, name) to check whether the name space of an OBJ has a name
- Getattr (OBJ, name) obtains a name in the name space of an obj.
- Setattr (OBJ, name, value) is the name of an obj.
- A name in space points to the object Vale.
- Delattr (OBJ, name) deletes a name from the name space of obj.
- Vars (OBJ) returns the name space of an object. Represented by dictionary
- Locals () returns a local name space, represented by dictionary
- Globals () returns a global name space, represented by dictionary.
- Type (OBJ) to view the type of an OBJ
- Isinstance (OBJ, CLS) Check if obj is a CLS instance
- Issubclass (subcls, supcls) Check whether subcls is a subclass of supcls
Help (OBJ) online help. If obj is of any type callable (OBJ), check whether an OBJ can call repr (OBJ) Like a function to obtain the expression string of obj, you can use this string eval to recreate a copy eval_r (STR) of the object to represent a valid Python expression. This expression Dir (OBJ) is returned) view the visible name hasattr (OBJ, name) in the Name Space of OBJ to check whether name getattr (OBJ, name) exists in the name space of an obj) get a name setattr (OBJ, name, value) in the Name Space of OBJ, and point a name in the name space of OBJ to the object delattr (OBJ, name) of Vale) delete A name vars (OBJ) from the name space of OBJ and return the name spa of an object. CE. Use dictionary to represent locals () to return a local name space, use dictionary to represent globals () to return a global name space, and use dictionary to represent type (OBJ) to view an isinstance (OBJ, CLs) Check if obj is a CLS instance issubclass (subcls, supcls) Check if subcls is a supcls subclass
Type conversion functions
Python code
- CHR (I) converts an ASCII value into a character
- Ord (I) converts a character or Unicode character into an ASCII value.
- Oct (x) converts integer x to a string represented by octal.
- Hex (x) converts the integer x into a hexadecimal string.
- STR (OBJ) returns the description of the OBJ string.
- List (SEQ) converts a sequence into a list
- Tuple (SEQ) converts a sequence into a tuple
- Dict () and dict (list) are converted into a dictionary
- INT (X) to an integer
- Convert long (X) to a long interger
- Float (X) is converted into a floating point number.
- Complex (X) to plural
- Max (...) calculates the maximum value.
- Min (...) calculates the minimum value.
CHR (I) converts an ASCII value to an ord (I) character to an ASCII value OCT (X) convert integer X to the string hex (x) represented by octal to convert integer X to the string STR (OBJ) represented by hexadecimal notation to obtain the string description list (SEQ) of obj) converts a sequence into a list tuple (SEQ), converts a sequence into a tuple dict (), and converts a dict (list) to a dictionary int (X) convert to an integer long (x) into a long interger float (x) into a floating point complex (x) into a plural max (...) maximum min (...) minimum value
Built-in function used to execute the program: complie if a piece of code is frequently used, it will compile and run faster.
Operating System-related calls
Python code
- System-related information modulesImportSys
- SYS. argv is a list containing all command line parameters.
- SYS. stdout SYS. stdin SYS. stderr indicates the standard input and output file objects with incorrect output.
- SYS. stdin. Readline () reads a row from the standard input SYS. stdout. Write ("A") and outputs
- SYS. Exit (exit_code) Exit the program
- SYS. modules is a dictionary that indicates all available modules in the system.
- SYS. Platform: Get the Operating System Environment
- SYS. path is a list that specifies the path of all modules and packages.
System-related information module import sys. argv is a list that contains all command line parameters. SYS. stdout sys. stdin sys. stderr indicates the standard input and output file objects with incorrect output. SYS. stdin. readline () reads a row of SYS from the standard input. stdout. write ("A") screen output a sys. exit (exit_code) Exit program sys. modules is a dictionary that indicates all available module sys in the system. platform to obtain the operating system environment sys. PATH is a list that specifies the path of all modules and packages.
Python code
- Operating System-related calls and operationsImportOS
- OS. Environ a dictionary contains the ing relationship of Environment Variables
- OS. Environ ["home"] To get the value of environment variable home
- OS. chdir (DIR) changes the current directory OS. chdir ('d: // outlook ')
- Note that escape is used in windows.
- OS. getcwd () to get the current directory
- OS. getegid () Get the valid group id OS. getgid () Get the group ID
- OS. getuid () get user id OS. geteuid () get valid user ID
- OS. setegid OS. setegid () OS. seteuid () OS. setuid ()
- OS. getgruops () to get the user group name list
- OS. getlogin () to get the User Login Name
- OS. getenv: Get the environment variable
- OS. putenv: Set Environment Variables
- OS. umask setting umask
- OS. System (CMD) uses the system call to run the CMD command
Operating System-related calls and Operations import osos. environ a dictionary contains the OS ing relation OS of environment variables. environ ["home"] can get the OS value of the environment variable home. chdir (DIR) changes the current directory OS. chdir ('d: // outlook ') Note that escape OS is used in windows. getcwd () to get the current directory OS. getegid () to get the valid group id OS. get the group id OS by getgid. getuid () to get the user id OS. geteuid () to obtain the valid user id OS. setegid OS. setegid () OS. seteuid () OS. setuid () OS. getgruops () obtains the user group name list OS. getlogin () obtains the User Logon Name OS. get the OS environment variable by getenv. putenv: Set the OS environment variable. set umask OS. system (CMD) uses the system call to run the CMD command
Instance
Python code
- OS. mkdir ('/tmp/xx ')
- OS. System ("Echo 'hello'>/tmp/XX/a.txt ")
- OS. listdir ('/tmp/xx ')
- OS. Rename ('/tmp/XX/a.txt', '/tmp/XX/B .txt ')
- OS. Remove ('/tmp/XX/B .txt ')
- OS. rmdir ('/tmp/xx') <span style = "white-space: normal"> </span>
os.mkdir('/tmp/xx') os.system("echo 'hello' > /tmp/xx/a.txt") os.listdir('/tmp/xx') os.rename('/tmp/xx/a.txt','/tmp/xx/b.txt') os.remove('/tmp/xx/b.txt') os.rmdir('/tmp/xx')
Python code
- #! /Usr/bin/Python
- ImportOS, sys
- Cmd = SYS. stdin. Readline ()
- WhileCMD:
- OS. System (CMD)
- Cmd = SYS. stdin. Readline ()