The os module provides dozens of functions to interact wieh OS.
123456
>>>importos>>>os.getcwd()# Return current working directory'C:\\Python35'>>>os.chdir('/server/accesslogs')# Change current working directory>>>os.system('mkdir today')# Run the command "mkdir" in system shell0
Be usre to use import os rather than from os import *
The built-in dir() and help() functions are useful.
12
>>>dir(os)# return all module functions>>>help(os)# return manual page.
sys module has attributes for stdin, stdout, and stderr
12
>>>sys.stderr.write('Warning, log file not found starting a new \n')Warning,logfilenotfoundstartinganewone
String Pattern Matching
re module provides regular expression tools for string processing.
123456
>>>importre>>>re.findall(r'\bf[a-z]*','which foot or hand fell fastest')['foot','fell','fastest']>>>re.sub(r'(\b[a-z]+) \1',r'\1','cat in the the hat')'cat in the hat'
When simple capabilities are needed, string methods are preferred
12
>>>'tea for too'.replace('too','two')'tea for two'
Mathematics
math module gives access to C library functions for loating point math.
random module provides tools for making random selections.
123456789101112
>>>importrandom>>>random.choice(['apple','pear','banana'])'apple'>>>random.sample(range(100),10)# find 10 sampling without replacement[30,83,16,4,8,81,41,50,18,33]>>>random.random()# random float0.17970987>>>random.randrange(6)# random integer chosen from range(6)4
statistics module calculates basic statistical properties of numeric data
urllib.request can retrieve data from URLs and smtplib can send mails.
12345678
>>>fromurllib.requestimporturlopen>>>withurlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl')asresponse:forlineinresponse:line=line.decode('utf-8')if'EST'inlineor'EDT'inline:# look for eastern timeprint(line)<BR>Nov.25,09:43:32PMEST
If there is a mailserver running on localhost
12345678
server=smtplib.SMTP('localhost')server.sendmail('a@example.org','b@example.org',"""This is mail content.To: b@example.orgFrom: a@example.org""")server.quit()
Dates and Times
datatime module supports dates, times and time zone
1234567891011
>>>fromdatetimeimportdate>>>now=date.today()>>>nowdatetime.date(2003,12,2)>>>now.strftime("%m-%d0%y, %d%b % Y is a %A on the %d day of %B.")'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December>>>birth=date(1964,7,31)>>>age=now-birthday>>>age.days14368
Data Compression
Use module zlib, gzip, bz2, lzma, zipfile and tarfile archive and compress data.
1234567891011
>>>importzlib>>>s=b'witch which has which witches wrist watch'>>>len(s)41>>>t=zlib.compress(s)>>len(t)37>>>zlib.decompress(t)b'witch which has which witches wrist watch'>>>zlib.crc32(s)226805979
Performance Measurement
timeit can quickly demonstrate a modest performance advantage
123456
>>>fromtimeitimportTimer>>>Timer('t=a;a=b;b=t','a=1;b=2').timeit()# Test code 10.5753>>>Timer('a,b=b,a','a=1; b=2').timeit()# Test code 20.5496
Compared with timeit moduel which can test in fine level of granularity, profile and pstats can test large block of codes