Python's common modules

Source: Internet
Author: User

Module:

The module is essentially a Python file that writes the Python code inside the module.

Module Categories:

Standard library: Python built-in

Open Source module: third party

Custom modules: Write your own

One, OS, sys module

ImportOS, sysPrint(OS.GETCWD ())#Get current directoryOs.chmod ("/usr/share", 7)#Add permissions to the/usr/share directoryPrint(Os.curdir)#current directoryPrint(Os.pardir)#Parent DirectoryPrint(Os.makedirs ("/usr/local/mysql"))#Create a directory recursively when the parent directory does not existPrint(Os.removedirs ("/usr/local/mysql"))#recursively delete empty directoriesPrint(Os.mkdir ("New"))#Create a folderOs.rename (" Old","New")# RenamingPrint(Os.path.join ("/root",'MySQL','Rack.sql'))#stitching into a pathPrint(Os.path.split ("/usr/mysql/123.txt"))#split path and file namePrint(OS.SEP)#path delimiter for current operating systemPrint(OS.LINESEP)#line breaks for the current operating systemPrint(OS.PATHSEP)#The delimiter for each path in the current system's environment variables, Linux is:, Windows is;  Print(Os.environ)#environment variables for the current systemPrint(Os.path.abspath (__file__))#Get absolute pathPrint(sys.version)#Get System versionPrint(SYS.ARGV)#command line Arguments list, the first element is the path of the program itselfPrint(Sys.path)#returns the search path for the module, using the value of the PYTHONPATH environment variable when initializingPrint(Sys.platform)#returns the operating system nameSys.stdout.write ('Please :')#output A Word to the screenPrint(sys.maxsize)#Maximum Value

Second, random module

Importrandom, StringPrint(Random.random ())#Random floating point number, default is 0~1, cannot specify range of valuesPrint(Random.randint (1,18))#randomly take integersPrint(Random.randrange (1,28))#randomly produces a rangePrint(Random.choice ('sjdkf93f')#random selection of an elementPrint(Random.sample ('Hello', 3))#randomly take 3 of elementsPrint(Random.uniform (1, 9))#Random floating point number, you can specify the range of valuesF= [1, 2, 3, 4, 5]random.shuffle (f)#Scramble OrderPrint(f)Print(string.ascii_letters+string.digits)#all the numbers and letters

Three, Time&timedate module

There are three ways to represent time, one is timestamp, one is format time, one is time tuple

ImportTime , TimedatePrint(Time.timezone ())#time difference from standard time, unit is SPrint(Time.time ())#gets the current timestampPrint(Time.sleep (1))#rest 1sPrint(Time.gmtime ())#convert timestamp to time tuple, default time stamp for standard Time zone if not passedPrint(Time.localtime ())#Converts the timestamp to a time tuple and, if not passed, the timestamp of the current time zone by default

Print(Time.mktime (Time.localtime ()))#convert a time tuple to a timestampPrint(Time.strftime ("%y%n%d%h%m%s"))#convert a time tuple to a formatted output stringPrint(Time.strptime ("20170908 182719","%y%m%d%h%m%s"))#convert formatted time to a time tuplePrint(Datetime.datetime.now ())#current time formatted output Print(Datetime.datetime.now () +datetime.timedelta (3))#3 days from the time Print(Datetime.datetime.now () +datetime.timedelta (-3))#3 days before the time

Python's common modules

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.