Introduction to common Python modules and operating system-related calls and operations

Source: Internet
Author: User

Built-in modules (which can be used directly without import) Common built-in functions:

 

Python code
  1. Help (OBJ) online help. obj is of any type.
  2. Callable (OBJ) to check whether an OBJ can be called like a function
  3. Repr (OBJ) to get the OBJ representation string. You can use this string eval to recreate a copy of this object.
  4. Eval_r (STR) indicates a valid Python expression. This expression is returned.
  5. Dir (OBJ) view the name visible in the name space of OBJ
  6. Hasattr (OBJ, name) to check whether the name space of an OBJ has a name
  7. Getattr (OBJ, name) obtains a name in the name space of an obj.
  8. Setattr (OBJ, name, value) is the name of an obj.
  9. A name in space points to the object Vale.
  10. Delattr (OBJ, name) deletes a name from the name space of obj.
  11. Vars (OBJ) returns the name space of an object. Represented by dictionary
  12. Locals () returns a local name space, represented by dictionary
  13. Globals () returns a global name space, represented by dictionary.
  14. Type (OBJ) to view the type of an OBJ
  15. Isinstance (OBJ, CLS) Check if obj is a CLS instance
  16. 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
  1. CHR (I) converts an ASCII value into a character
  2. Ord (I) converts a character or Unicode character into an ASCII value.
  3. Oct (x) converts integer x to a string represented by octal.
  4. Hex (x) converts the integer x into a hexadecimal string.
  5. STR (OBJ) returns the description of the OBJ string.
  6. List (SEQ) converts a sequence into a list
  7. Tuple (SEQ) converts a sequence into a tuple
  8. Dict () and dict (list) are converted into a dictionary
  9. INT (X) to an integer
  10. Convert long (X) to a long interger
  11. Float (X) is converted into a floating point number.
  12. Complex (X) to plural
  13. Max (...) calculates the maximum value.
  14. 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
  1. System-related information modulesImportSys
  2. SYS. argv is a list containing all command line parameters.
  3. SYS. stdout SYS. stdin SYS. stderr indicates the standard input and output file objects with incorrect output.
  4. SYS. stdin. Readline () reads a row from the standard input SYS. stdout. Write ("A") and outputs
  5. SYS. Exit (exit_code) Exit the program
  6. SYS. modules is a dictionary that indicates all available modules in the system.
  7. SYS. Platform: Get the Operating System Environment
  8. 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
  1. Operating System-related calls and operationsImportOS
  2. OS. Environ a dictionary contains the ing relationship of Environment Variables
  3. OS. Environ ["home"] To get the value of environment variable home
  4. OS. chdir (DIR) changes the current directory OS. chdir ('d: // outlook ')
  5. Note that escape is used in windows.
  6. OS. getcwd () to get the current directory
  7. OS. getegid () Get the valid group id OS. getgid () Get the group ID
  8. OS. getuid () get user id OS. geteuid () get valid user ID
  9. OS. setegid OS. setegid () OS. seteuid () OS. setuid ()
  10. OS. getgruops () to get the user group name list
  11. OS. getlogin () to get the User Login Name
  12. OS. getenv: Get the environment variable
  13. OS. putenv: Set Environment Variables
  14. OS. umask setting umask
  15. 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
  1. OS. mkdir ('/tmp/xx ')
  2. OS. System ("Echo 'hello'>/tmp/XX/a.txt ")
  3. OS. listdir ('/tmp/xx ')
  4. OS. Rename ('/tmp/XX/a.txt', '/tmp/XX/B .txt ')
  5. OS. Remove ('/tmp/XX/B .txt ')
  6. 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
  1. #! /Usr/bin/Python
  2. ImportOS, sys
  3. Cmd = SYS. stdin. Readline ()
  4. WhileCMD:
  5. OS. System (CMD)
  6. Cmd = SYS. stdin. Readline ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.