python built-in module described (a)

 This paper describes the module list is as follows:

  1. the
  2. sys
  3. re
  4. time
  5. datetime
  6. random
  7. shutil
  8. Subprocess

os module 

  1. os.getcwd () Gets the current working directory
  2. os.chdir ( "/ path / to") directory to / path / to the directory, the shell equivalent to the cd command
  3. os.makedirs ( 'path1 / path2') to create a multi-layer directory (the directory may be simultaneously multilayer absent), corresponding to the shell command mkdir -p
  4. os.removedirs ( "path1 / path2") Delete empty directory, if the directory is not empty it will error, first remove the path2 directory, if path1 has become an empty directory, and will delete path1, to know the directory of the first layer ( path1 i.e. in the example) is stopped
  5. os.mkdir ( "path") to create a flat directory path, the equivalent of the shell of mkdir, Note: path of the directory where the parent directory must already exist
  6. os.rmdir ( "path") Delete single-directory path, the equivalent of rmdir shell
  7. os.listdir ( "path") lists all the files and subdirectories in the specified directory, including hidden files, the return value is a list
  8. os.remove ( "file") delete a single file
  9. os.rename ( "a", "b") Rename (file or directory)
  10. os.stat ( "a") obtaining a (file or directory) of all the information: The return value is a class os.stat_result
  11. os.system ( "shell command") to run shell commands, displayed directly operating results, the return value is the successful implementation of the program code, the return value of non-operating results of the shell, 0 for success, non-zero failure

os.path down method

  1. os.path.abspath (path) returns the absolute path path
  2. the os.path.realpath (path) Returns the path of the absolute path, and the difference is abspath, if a path is connected, the return path is an absolute path connected
  3. oa.path.split ( "path") into the directory path and file name, the return value is binary ancestral
  4. os.path.dirname ( "path") Returns the directory path where
  5. os.path.basename ( "path") Returns the file path name
  6. os.path.exists ( "a") if there is a return True; if there is not a return False; a file can also be a directory
  7. os.path.isabs ( "path") if the path is the absolute path, returns True
  8. os.path.isfile ( "path") if the path is an existing file and returns True. Otherwise it returns False
  9. os.path.isdir ( "path") if the path is an existing directory, it returns True. Otherwise it returns False
  10. os.path.join (path1 [, path2 [, ...]]) will be a combination of a plurality of paths after the return, a parameter before the first absolute path will be ignored
  11. os.path.getsize ( "a") returns a size (if a file is the file size; if a is a directory, the inode list is occupied space in the directory, not the directory within the space occupied by the file size; If a is a symbolic link, the path name is the number of bytes of the linked file)
  12. os.path.getatime ( "a") returns a file or directory pointed to by the last access time
  13. os.path.gettime ( "a") returns a file or directory pointed to by the last rights to modify the time
  14. os.path.getmtime ( "a") returns a file or directory pointed to by the last content modification time

No small brackets

  1. os.curdir return value is that relative to the current directory, pay attention. "": no parentheses
  2. os.pardir return value is "..", i.e. on the opposite path of a directory, Note: no parentheses
  3. Os.sep operating system output path delimiter for the next win "\\", under Linux is "/", no parentheses
  4. os.linesep current operating system output line terminator, to win the next "\ t \ n", as "\ n" under Linux, without parentheses
  5. os.pathsep output string into the current operating system for the file path, no parentheses
  6. os.name output current using the platform, win -> 'nt'; Linux -> 'posix'
  7. os.environ acquisition system environment variables, the return value is a class, the value of which may be invoked by various methods return values

sys module

  1. sys.argv command list data, the first element of the program itself
  2. the sys.exit (a) to exit the program, if it is a normal exit is false, if true, abnormal exit, and a return (false may be 0, False, and so the empty string; true data is not empty)
  3. sys.version print python interpreter version
  4. sys.path return path search module, which corresponds to a type of list, the list can be modified by a method
  5. sys.platform returns the name of the operating system
  6. sys.stdout.write ( "a") standard output print a (also corresponding to stderr)

re module

Re module functions described

  1. re.match (pattern, string) to start from scratch match
  2. re.search (pattern, string) match (if pattern adds ^, just match the same function)
  3. re.findall (pattern, string) to match all the characters in list form return
  4. re.split (pattern, string) string according to the division pattern, return to the list, the list element does not contain pattern
  5. re.sub (pattern, replace, string) in the string to replace the replacement pattern

Regular Expressions Symbol Meaning

  1. . "" The default matches any character except \ n, if the specified flag DOTALL, it matches any character, including newline
  2. "^" Matches the beginning characters, if specified flags MULTILINE, this (r "^ a", "\ nabc \ neee", flags = re.MULTILINE) can also be matched (not designated flags, "\ nabc \ neee" is line)
  3. The match ends in "$" character, if specified flags MULTILINE, this ( "foo $", "bfoo \ nsdfsf", flags = re.MULTILINE) can match, ibid.
  4. "*" Matches an asterisk before the character zero or more times
  5. "+" Matches before a character one or more times
  6. "?" Matches before a character 0 or 1 times
  7. "{m}"       matching prior character at least m
  8. "{M, n}" character before a match to m n times (m times least i.e., up to n times)
  9. "|" Matching | left or | the right character
  10. "(...)" matched packet, the packet sequence: left to right, from outside to inside
    1. Grouping can also be subdivided, general grouping, grouping named after the quote, forward certainly assert, to affirm the assertion, negative assertion forward, backward negative assertions
    2. Ordinary packet , ..... are matched to information acquired by 1, as re.search (r "(abc)" , "abcdefg"). Group (1)
    3. Naming packet , is acquired by matching the name information to the packet, such as(?P<name>正则表达式),这里的命名就是name,通过groupdict("name")函数获取捕获值
    4. Other much used
  11. [...] person matching within a character extender, if (i.e., [^]), plus any non brackets The representative character matching a front brackets
  12. "\ A" matches only character from the beginning, re.search ( "\ Aabc", "alexabc") is not a match
  13. Ending "\ Z" matches the character, with the $
  14. "\ D" matching digits [0-9]
  15. "\ D" match non-digital
  16. "\ W" Match [A-Za-z0-9]
  17. "\ W" matches [A-Za-z0-9] non
  18. "\ S" matches blank characters, \ t, \ n, \ r
  19. "\ S" match \ s complement of
    1. \ D and \ D, \ w and \ W, \ s and \ S are each the complement of the other and can be combined with all the characters

time module

  • There are three ways in the time indicated in the time module:... A time stamp, the formatted time string 2, 3 tuples (struct_time, 9 elements)
  • UTC (Coordinated Universal Time, Coordinated Universal Time), the world standard time, China is UTC + 8; when DST (Daylight Saving Time) Daylight Saving Time
  • Timestamp (timestamp): Generally speaking, the time stamp indicates the amount from the beginning of January 1970 0:00:00 1st calculated in seconds
  • Ganso (struct_time) has nine elements, tm_year: Year, tm_mon: month, tm_mday: date, tm_hour: hours, tm_min: minutes, tm_sec: seconds, tm_wday: The first few days of the week (Monday is represented by 0), tm_yday: the first day of the year, tm_isdst: whether daylight saving time

Function Introduction

  Various types of conversion time in time, reference: https: //blog.51cto.com/egon09/1840425

 

  1. the time.time () the current time, the type of timestamp (timestamp)
  2. time.sleep (timestamp) program sleep in seconds
  3. time.gmtime ([timestamp]) type struct_time; not pass parameters, the current utc time; if you pass parameters, compared with the January 1970 0:00:00 1st time after the timestamp utc time
  4. time.localtime ([timestamp]) type struct_time; not pass parameters, current local time; if you pass parameters, compared with the January 1, 1970 0:00:00 elapsed time after the timestamp of the local time
  5. time.asctime ([struct_time]) is a string type, the format of " 'Wed Jul 17 08:40:40 2019'" "; not to pass parameters, parsing the current time; transfer parameters, time resolution parameters
  6. time.ctime ([timestamp]) is a string type, the format of " 'Wed Jul 17 08:40:40 2019'" "; not to pass parameters, parsing the current time; transfer parameters, time resolution parameters (different given above different type parameters are passed)
  7. the time.strptime (string, format) struct_time return type, according to the format string parsing, formatting format described below
  8. time.strftime (format [, tuple] Returns the string, the string is converted to time format in accordance with the format form, is not specified tuple time, the default localtime

 

    1. % Y represents two-digit year (00-99)
    2. % Y represents a four-digit year (000-9999)
    3. % M (01-12)
    4. Within a% d day of the month (0-31)
    5. % H 24 hours (0-23) manufactured by h
    6. % I 12 hours hour (01-12)
    7. % M number of minutes (00-59)
    8. % S seconds (00-59)
    9. % A week simplify local name
    10. % A full weekday name local
    11. % B local simplify month name
    12. % B Full month name of the local
    13. % C represents the corresponding local date and time representation, and returns the same time.ctime
    14. One day (001-366)% j years
    15. % P local AM or PM equivalent character
    16. % U week number of the year (00-53) for the week beginning Sunday
    17. % W week (0-6), Sunday is the start of week
    18. % W week number of the year (00-53) for the week beginning Monday
    19. % X indicates the corresponding local date
    20. % X indicates the corresponding local time
    21. #% Z name garbled current time zone
    22. %%% Number itself

 

datetime module

  1. datetime.dateime.now () Returns the current time, type A datetime.datetime,
  2. the datetime.timedelta () This function is not used alone, should now fit function, addition and subtraction of time, such as: datetime.dateime.now () + datetime.timedelta (-3)
    1. Timedelta parameters are: days = 0, seconds = 0, microseconds = 0, milliseconds = 0, minutes = 0, hours = 0, weeks = 0

Date of function as well as calendar (calendar) module, for details see: https://docs.python.org/3/library/calendar.html

random module

  1. random.random () Returns the floating-point number, interval [0.0 1) does not comprise 1 ---
  2. random.uniform (a, b) returns the floating-point number, customizable interval, [a, b)
  3. random.int (a, b) returns the integer interval [a, b]
  4. random.randrange (a, b) returns an integer, as the interval [a, b) the nature and range interval function
  5. The random.choice (seq) Returns a seq element of
  6. random.shuffle (x) reshuffle x, i.e. out of sequence, x is a list of

shutil 模块 shutil

Advanced file, folder processing

  1. shutil.copyfilebj (src, dst) copy the file object, src and dst must be a file handle (i.e., the file handle open)
  2. shutil.copyfile (src, dst) copy files, src and dst is the file name (call copyfilebj achieved)
  3. shutil.copymode (src, dst) to copy the file permissions, content, groups, users were unchanged, src and dst is a file, and must be present
  4. Status information shutil.copystat (src, dst) copy of the document, including mode, atime, ctime, flags, etc.
  5. shutil.copy (src, dst) to copy files and permissions, src and dst is a file
  6. shutil.copy2 (src, dst) and the state of copying files, src and dst are files
  7. shutil.copytree (src, dst) copy files recursively
  8. shutil.rmtree (path) recursively delete files
  9. shutil.move (src, dst) to move files recursively

subprocess module

Run directly run method is recommended if you use the more advanced can be used directly Popen interfaces; If you want a result of the program, recommended getstatusoutput (cmd)

  1. subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None)
    1. args: marked command to be executed, must be a string, the string parameter needs to be a list, only if the shell parameter is true, then the parameters and commands can be placed in a string, because it is through the system shell carried out
      1.   For example: If there are parameters that must be [ "ls", "- al "], no parameters can write "ls"; if the shell is True, can be written as "LS -a "
    2. stdin, stdout and stderr: standard child process input, output, and error. Its value can be subprocess.PIPE, subprocess.DEVNULL, a file descriptor already exists, the object file is already open or None. subprocess.PIPE represents the creation of a new pipeline to the child process. subprocess.DEVNULL indication os.devnull. The default is None, represents nothing. Further, stderr to stdout may be incorporated together in the output.
    3. timeout: Set command timeout. If the command times out, the child will be killed, and pop TimeoutExpired exception.
    4. check: If this parameter is set to True, and the process exit status is not 0, the pop CalledProcessError exception.
    5. encoding: If this parameter is specified, stdin, stdout and stderr string data may be received, and in that has been encoded. Otherwise receive only type of data bytes.
    6. shell: If this parameter is True, will be performed by the operating system shell commands specified.
  2. subprocess.getstatusoutput (cmd) return value is a binary tuple is a first exitcode (0 run successfully on behalf of, the other for failure), the second operation result is returned cmd
    1. There is also a similar function subprocess.getoutput (cmd) run only return results

Please refer to the remainder of: Python built-in module described (b)         

Guess you like

Origin www.cnblogs.com/lufeng20141208/p/11192930.html
Recommended