python-Common built-in modules

Source: Internet
Author: User
Tags function prototype

Introduction to commonly used built-in modules
Os,sys,time,datetime,json/pickle,shutil,random


One. os

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/A4/BD/wKioL1mwtrawjKurAAAJ9w_dRlI511.png "style=" float : none; "title=" screenshot from 2017-09-07 10-11-07.png "alt=" Wkiol1mwtrawjkuraaaj9w_drli511.png "/>

1. Switch directories to see the current working directory OS.GETCWD

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/06/0C/wKiom1mwttfzGSDBAAAu2K8hiQ0054.png "style=" float : none; "title=" screenshot from 2017-09-07 10-11-18.png "alt=" Wkiom1mwttfzgsdbaaau2k8hiq0054.png "/>


2. Current directory, parent directory Os.curdir Os.pardir

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A4/BD/wKioL1mwtryzHQavAAAhnJmLTRs714.png "style=" float : none; "title=" screenshot from 2017-09-07 10-11-51.png "alt=" Wkiol1mwtryzhqavaaahnjmltrs714.png "/>


3. Create a directory Os.mkdir

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/06/0C/wKiom1mwtt2DxyxTAACHaeLzg4Y061.png "style=" float : none; "title=" screenshot from 2017-09-07 10-13-42.png "alt=" Wkiom1mwtt2dxyxtaachaelzg4y061.png "/>


4. deleting files Os.remove

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/A4/BD/wKioL1mwtsDxJsBdAABJP5RKeRM571.png "style=" float : none; "title=" screenshot from 2017-09-07 10-19-42.png "alt=" Wkiol1mwtsdxjsbdaabjp5rkerm571.png "/>

5. Renaming Os.rename

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/0C/wKiom1mwtuDA_OfFAAAzTjcnS4o667.png "style=" float : none; "title=" screenshot from 2017-09-07 10-21-02.png "alt=" Wkiom1mwtuda_offaaaztjcns4o667.png "/>

6. Execute shell command Os.system

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/A4/BD/wKioL1mwtsWycomXAAAVMDbH8lA333.png "style=" float : none; "title=" screenshot from 2017-09-07 10-38-00.png "alt=" Wkiol1mwtswycomxaaavmdbh8la333.png "/>


7. Return the directory and file name of the path os.path.split

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/A4/BD/wKioL1mwuH7g1sWKAAAgW_sWnhQ424.png "style=" float : none; "title=" screenshot from 2017-09-07 11-06-15.png "alt=" Wkiol1mwuh7g1swkaaagw_swnhq424.png "/>


8. Determine if the specified object is a file, directory Os.path.isfile Os.path.isdir

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/06/0C/wKiom1mwuJ6DkSIjAAAys1foQmg095.png "style=" float : none; "title=" screenshot from 2017-09-07 11-07-00.png "alt=" Wkiom1mwuj6dksijaaays1foqmg095.png "/>


9. Get the size of the file , If it is a directory, return 0 os.path.getsize

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A4/BD/wKioL1mwuH6wctX2AAAZVeZrR4M933.png "style=" float : none; "title=" screenshot from 2017-09-07 11-08-33.png "alt=" Wkiol1mwuh6wctx2aaazvezrr4m933.png "/>

10. Connection directory and file name Os.path.join

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/06/0C/wKiom1mwuJ7hiyvPAAAdza70KVM130.png "style=" float : none; "title=" screenshot from 2017-09-07 11-09-23.png "alt=" Wkiom1mwuj7hiyvpaaadza70kvm130.png "/>


11. Determine if the directory exists os.path.exists

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A4/BD/wKioL1mwuzHioC3PAAAXSZfarTA190.png "title=" Screenshot from 2017-09-07 11-20-24.png "alt=" Wkiol1mwuzhioc3paaaxszfarta190.png "/>


Two. sys

1.sys.path is a list that indicates all paths to find Module,package

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A4/BE/wKioL1mwwMiRYiwlAADI6vsruVs768.png "title=" Screenshot from 2017-09-07 11-44-43.png "alt=" Wkiol1mwwmiryiwlaadi6vsruvs768.png "/>


2.SYS.ARGV is a list that contains all the command-line arguments

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/A4/BE/wKioL1mwwFSDNkOoAAAYVvWd5tc595.png "title=" Screenshot from 2017-09-07 11-42-26.png "alt=" Wkiol1mwwfsdnkooaaayvvwd5tc595.png "/>


3.sys.stdin.readline () reads a line of screen output from standard input a

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/A4/BE/wKioL1mwwVegdV5JAAAlyYHZW84094.png "title=" Screenshot from 2017-09-07 11-47-15.png "alt=" Wkiol1mwwvegdv5jaaalyyhzw84094.png "/>


4.sys.modules is a dictionary that represents all the available module in the system

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A4/BE/wKioL1mwwbvzXiqaAABJQF1dHT0265.png "title=" Screenshot from 2017-09-07 11-48-47.png "alt=" Wkiol1mwwbvzxiqaaabjqf1dht0265.png "/>


5.sys.platform getting running operating system environment
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/06/0D/wKiom1mwwgeQZksxAAAWojs5vsI235.png "title=" Screenshot from 2017-09-07 11-49-34.png "alt=" Wkiom1mwwgeqzksxaaawojs5vsi235.png "/>


Three. Time

1.time.time (): Returns the timestamp of the current time

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A4/BE/wKioL1mwxCLgU225AAAgPL-zTDA320.png "title=" Screenshot from 2017-09-07 11-59-00.png "alt=" Wkiol1mwxclgu225aaagpl-ztda320.png "/>


2.time.localtime ([secs])
Converts a timestamp to the struct_time of the current time zone, which is the time array format

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A4/BE/wKioL1mwxI7js0OUAAA0cQ0qLfM941.png "title=" Screenshot from 2017-09-07 11-59-58.png "alt=" Wkiol1mwxi7js0ouaaa0cq0qlfm941.png "/>


3.time.gmtime ([secs])
Convert a timestamp to the struct_time of the UTC time Zone

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/06/10/wKiom1mw62PwClVzAAA9Le4ImJE179.png "title=" Screenshot from 2017-09-07 14-46-15.png "alt=" Wkiom1mw62pwclvzaaa9le4imje179.png "/>

4.time.mktime (t): Converts a struct_time to a timestamp

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/06/10/wKiom1mw7MuBvRDnAAAb4ZBjQJQ746.png "title=" Screenshot from 2017-09-07 14-51-44.png "alt=" Wkiom1mw7mubvrdnaaab4zbjqjq746.png "/>


5.time.sleep (secs): Thread delays the specified time, the thread sleeps for a specified time, in seconds

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/06/10/wKiom1mw7RCTcClgAAAOv_6IS0M040.png "title=" Screenshot from 2017-09-07 14-53-03.png "alt=" Wkiom1mw7rctcclgaaaov_6is0m040.png "/>


6.time.clock ()

This function returns the current CPU time with the number of seconds that the function calculates in floating-point numbers.

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/A4/C1/wKioL1mw7UvyPmfsAAAWei18zb0895.png "title=" Screenshot from 2017-09-07 14-54-54.png "alt=" Wkiol1mw7uvypmfsaaawei18zb0895.png "/>


7.time.asctime ()
A tuple or struct_time representing time is represented as a form of ' Thu Sep 7 14:31:59 2017 '. If no argument is given, Time.localtime () is passed in as a parameter.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A4/C1/wKioL1mw7hqj0SUXAABELiXZntw804.png "title=" Screenshot from 2017-09-07 14-57-40.png "alt=" Wkiol1mw7hqj0suxaabelixzntw804.png "/>


8.time.ctime ([secs])
Converts a timestamp (a floating-point number in seconds) into the form of time.asctime (). If you specify a parameter, Time.time () is used by default as the parameter.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A4/C1/wKioL1mw8FfiZQ0TAAA7MOCNlME046.png "title=" Screenshot from 2017-09-07 15-07-32.png "alt=" Wkiol1mw8ffizq0taaa7mocnlme046.png "/>


9.time.strftime (format [, T])

Format: Formatting strings

[t] optional parameter is a Struct_time object

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A4/C2/wKioL1mw84DyyliTAABbHpCUV98426.png "title=" Screenshot from 2017-09-07 15-19-05.png "alt=" Wkiol1mw84dyylitaabbhpcuv98426.png "/>


10.time.strptime (String[,format])
Converts a format string into Struct_time. The function is an inverse operation of the time.strftime () function. The time strptime () function resolves a timing string to a time tuple based on the specified format. So the function returns a Struct_time object.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/06/11/wKiom1mw9Rrj5NoRAABG3pwl0Bs840.png "title=" Screenshot from 2017-09-07 15-27-39.png "alt=" Wkiom1mw9rrj5noraabg3pwl0bs840.png "/>


Four. DateTime

1. Get the current datetime

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/06/11/wKiom1mw9qORWI2zAAAONjd8P8E967.png "style=" float : none; "title=" screenshot from 2017-09-07 15-33-16.png "alt=" Wkiom1mw9qorwi2zaaaonjd8p8e967.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/A4/C2/wKioL1mw9oPC9QOIAAApP915kpw866.png "style=" float : none; "title=" screenshot from 2017-09-07 15-33-33.png "alt=" Wkiol1mw9opc9qoiaaapp915kpw866.png "/>


2. Get Date of day

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A4/C2/wKioL1mw9t2zRJWgAAAiooqa_kQ522.png "title=" Screenshot from 2017-09-07 15-35-43.png "alt=" Wkiol1mw9t2zrjwgaaaiooqa_kq522.png "/>


3. Get tomorrow

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/A4/C2/wKioL1mw9zDwWJM3AAAol8WYvIs405.png "title=" Screenshot from 2017-09-07 15-37-12.png "alt=" Wkiol1mw9zdwwjm3aaaol8wyvis405.png "/>


4. Three days before the current time

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/A4/C2/wKioL1mw94TCZInnAAAvwtgn2oQ657.png "title=" Screenshot from 2017-09-07 15-38-34.png "alt=" Wkiol1mw94tczinnaaavwtgn2oq657.png "/>


5. Get the start and end times of the day (00:00:00 23:59:59)

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/06/12/wKiom1mw-DXRHJgbAABVmJO1_q8461.png "title=" Screenshot from 2017-09-07 15-40-54.png "alt=" Wkiom1mw-dxrhjgbaabvmjo1_q8461.png "/>


Five. Json/pickle

Son and Pickle module, two modules for serialization

-JSON module for conversion between string and Python data types

-Pickle module for conversion between Python-specific types and Python data types

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/A4/C3/wKioL1mw_KzADxGUAAANmb48_5c361.png "style=" float : none; "title=" screenshot from 2017-09-07 15-59-35.png "alt=" Wkiol1mw_kzadxguaaanmb48_5c361.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/A4/C3/wKioL1mw_K2wzRfhAAA9r0H-s7I178.png "style=" float : none; "title=" screenshot from 2017-09-07 15-59-42.png "alt=" Wkiol1mw_k2wzrfhaaa9r0h-s7i178.png "/>


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/06/12/wKiom1mw_Wag6P22AABTde_aFZ4449.png "title=" Screenshot from 2017-09-07 16-03-07.png "alt=" Wkiom1mw_wag6p22aabtde_afz4449.png "/>


Six. Shutil

1.shutil.copyfile (SRC, DST) replication

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/A4/C4/wKioL1mxBWzREGT2AAANeljgbHo225.png "title=" Screenshot from 2017-09-07 16-37-48.png "alt=" Wkiol1mxbwzregt2aaaneljgbho225.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/06/13/wKiom1mxBTvzcG1MAAAsBoHSzkA177.png "style=" float : none; "title=" screenshot from 2017-09-07 16-36-25.png "alt=" Wkiom1mxbtvzcg1maaasbohszka177.png "/>


2.shutil.move (SRC, DST) move files or rename

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/13/wKiom1mxBg3g1zo4AAArSaXlUr4469.png "title=" Screenshot from 2017-09-07 16-40-01.png "alt=" Wkiom1mxbg3g1zo4aaarsaxlur4469.png "/>


3.shutil.get_archive_formats () See which packaging formats are supported

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/17/wKiom1mxOpKg_FIwAABSvxqdmrE424.png "title=" Screenshot from 2017-09-07 20-23-04.png "alt=" Wkiom1mxopkg_fiwaabsvxqdmre424.png "/>

4.shutil.copymode (SRC, DST) just replicates its permissions other things are not copied

5.shutil.copystat (SRC, DST) copy permissions, last access time, last modified time

6.shutil.copy (SRC, DST) copy a file to a file or a directory

7.shutil.copy2 (SRC, DST) copy files on the basis of copy the last access time and modification time are also copied, similar to cp–p things

8.shutil.copy2 (SRC, DST) if the file system in two locations is the same, it is equivalent to rename operation, just renaming; if it's not the same filesystem, it's a move operation.

9.shutil.rmtree (SRC) recursively deletes a directory and all content within the directory


Seven. Random

1.random.random () is used to generate a 0 to 1 random character point

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/06/14/wKiom1mxCcryg2qQAAAOVsuQqGA059.png "style=" float : none; "title=" screenshot from 2017-09-07 16-54-20.png "alt=" Wkiom1mxccryg2qqaaaovsuqqga059.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/A4/C5/wKioL1mxCaqwNy4SAAAZYmRRo0k554.png "style=" float : none; "title=" screenshot from 2017-09-07 16-55-14.png "alt=" Wkiol1mxcaqwny4saaazymrro0k554.png "/>



2.random.uniform function to generate a random number of points in a specified range

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/06/14/wKiom1mxCiHyyU1wAAAbRJZWZQA883.png "title=" Screenshot from 2017-09-07 16-57-06.png "alt=" Wkiom1mxcihyyu1waaabrjzwzqa883.png "/>

A function of 3.random.randint () to generate an integer within a specified range

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/A4/C5/wKioL1mxClHwH6XPAAAUAyGFdl4493.png "title=" Screenshot from 2017-09-07 16-58-23.png "alt=" Wkiol1mxclhwh6xpaaauaygfdl4493.png "/>


4.random.randrange's function prototype is: Random.randrange ([start], stop[, step]), gets a random number from the specified range, in the collection incremented by the specified cardinality

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/06/14/wKiom1mxCtiQ-hfCAAAWnrVAAeY257.png "title=" Screenshot from 2017-09-07 17-00-14.png "alt=" Wkiom1mxctiq-hfcaaawnrvaaey257.png "/>

5.random.choice gets a random element from the sequence.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/06/14/wKiom1mxC2KhEIp1AAAf__3k6OY987.png "title=" Screenshot from 2017-09-07 17-02-34.png "alt=" Wkiom1mxc2kheip1aaaf__3k6oy987.png "/>


A 6.random.sample function that randomly fetches fragments of a specified length from a specified sequence.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/A4/C5/wKioL1mxDHzjCFiPAAAfJrKm468327.png "title=" Screenshot from 2017-09-07 17-07-47.png "alt=" Wkiol1mxdhzjcfipaaafjrkm468327.png "/>


The 7.random.shuffle function is used to disrupt elements in a list

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/A4/C5/wKioL1mxEAGRd39AAAAwQSE_gQI395.png "title=" Screenshot from 2017-09-07 17-22-57.png "alt=" Wkiol1mxeagrd39aaaawqse_gqi395.png "/>


python-Common built-in modules

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.