Python Common Modules | Python

Source: Internet
Author: User

# Built-in modules, third-party modules, custom modules#Which path does the import module base on? Sys.path#You can add a path by Sys.path.append (");    ImportSYSImportOS Project_path= Os.path.dirname (Os.path.dirname (Os.path.abspath (__file__)) ) sys.path.append (Project_path)#1.sys | Python interpretersys.argv command line argument list, the first element is the program itself path Sys.exit (n) exit the program, exit gracefully (0) sys.version get python explanation Version information for the program sys.maxint the maximum int value Sys.path returns the search path of the module, using the value of the PYTHONPATH environment variable when initializing Sys.platform returns the operating system flat Table name Sys.stdin input related sys.stdout output related Sys.stderror error related#2.os | operating systemOS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work os.chdir ("dirname") Changes the current script working directory, equivalent to the shell under the CD Os.curdir returns the current directory: ('.') Os.pardir Gets the parent directory string name of the current directory: ('..') Os.makedirs ('Dir1/dir2') to generate a multi-level recursive directory Os.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir dirname os.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname os.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints a list of os.remove () deletes a file Os.rename ("oldname","New") Rename File/Catalog Os.stat ('Path/filename') Get File/directory information OS.SEP operating system-specific path delimiter, win under"\\", under Linux for"/"Os.linesep The line terminator used by the current platform, win under"\t\n", under Linux for"\ n"os.pathsep The string Os.name string used to split the file path indicates the current usage platform. Win-'NT'; Linux->'POSIX'Os.system ("Bash Command"run the shell command and directly display the Os.environ gets the system environment variable Os.path.abspath (path) returns the absolute path normalization OS.PATH.SP Lit (path) splits path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned.         The second element of Os.path.split (path) os.path.exists (path) returns true if path exists, or false if path does not exist Os.path.isabs (path) If path is an absolute path, returns true Os.path.isfile (path) If path is an existing file and returns True. Otherwise, false Os.path.isdir (path) returns True if path is an existing directory.  Otherwise return false Os.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored Os.path.getatime (path) returns the last Access time Os.path.getmtime (path) of the file or directory to which path is pointing. The last modification time of the file or directory to#3.hashlib#instead of the MD5 module and the SHA module,#mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm    ImportHashlib######### MD5 ########hash =hashlib.md5 ()#Help (hash.update)Hash.update (Bytes ('Admin', encoding='Utf-8'))    Print(Hash.hexdigest ())Print(Hash.digest ())######## SHA1 ########Hash=hashlib.sha1 () hash.update (bytes ('Admin', encoding='Utf-8'))    Print(Hash.hexdigest ())######### sha256 ########Hash=hashlib.sha256 () hash.update (bytes ('Admin', encoding='Utf-8'))    Print(Hash.hexdigest ())######### sha384 ########Hash=hashlib.sha384 () hash.update (bytes ('Admin', encoding='Utf-8'))    Print(Hash.hexdigest ())######### sha512 ########Hash=hashlib.sha512 () hash.update (bytes ('Admin', encoding='Utf-8'))    Print(Hash.hexdigest ())#4.random    ImportRandomPrint(Random.random ())Print(Random.randint (1, 2))    Print(Random.randrange (1, 10))        #Random Verification Code        ImportRandom Checkcode="'         forIinchRange (4): current= Random.randrange (0,4)            ifCurrent! =i:temp= Chr (Random.randint (65,90))            Else: Temp= Random.randint (0,9) Checkcode+=Str (temp)PrintCheckcode#5.re#6.json#for conversion between string and Python base data types#7.pickle#for conversion between Python-specific types and Python basic data types#8.logging#module for easy logging and thread-safe#9.time#timestamp after January 1, 1970 seconds, i.e.: Time.time ()#formatted string 2014-11-11 11:11, i.e.: Time.strftime ('%y-%m-%d ')#structured time tuples include: year, day, week, etc. time.struct_time is: time.localtime ()    Printtime.time ()PrintTime.mktime (Time.localtime ())PrintTime.gmtime ()#time-stamped parameters can be added    PrintTime.localtime ()#time-stamped parameters can be added    PrintTime.strptime ('2014-11-11','%y-%m-%d')           PrintTime.strftime ('%y-%m-%d')#Default Current Time    PrintTime.strftime ('%y-%m-%d', Time.localtime ())#Default Current Time    Printtime.asctime ()PrintTime.asctime (Time.localtime ())PrintTime.ctime (Time.time ())Importdatetime" "Datetime.date: A class that represents a date. Common attributes are year, month, day Datetime.time: The class that represents the time. Commonly used properties are hour, minute, second, microsecond datetime.datetime: Represents the date time Datetime.timedelta: Represents the time interval, that is, the length between two time points Timedelta (    [Days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, Weeks]] Strftime ("%y-%m-%d")" "    ImportdatetimePrintDatetime.datetime.now ()PrintDatetime.datetime.now ()-Datetime.timedelta (days=5)

Python Common Modules | Python

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.