Here we discuss some of the modules which are very useful to a programmer now.
PageContents
Modules of python that are discussed in this post.
Operating System Interface
Python can also interact with the operating system you are using. For a different operating system, different python software will be available so there is no problem in accessing the python.
The os module will provide dozens of commands to interact with the operating system.
If you want to write or read files you can use open() method, manipulate paths using os.path module, create a file, or a directory using shutil module.
All the functions in this module will raise OSError in case of invalid or inaccessible file or path names.
>>>import os
>>>os.getcwd()
'C:\\python38'
>>>os.chdir('\system\files\')
>>>os.system('mkdir programs')
getcwd() will give you the current working directory of the system. chdir() will change you the path of the current directory. system() will be used as a shell you can give any commands.
For file and directory management tasks shutil module will provide you an interface which is easy to use.
>>>import shutil
>>>shutil.copyfile('data.db','archive.db')
'archive.db'
>>>shutil.move('/users/programs','Python38')
'Python38'
Be sure you use import os style instead of from os import *. This will make os.open() from shadowing the built-in function open() which are different.
To know all the module functions use the command dir(os) and to know the module's docstring use the command help(os).
Command-line Arguments
Common utility scripts often need to process command-line arguments. These arguments are stored in the
sys
module’s argv attribute as a list. For instance the following output results from running python demo.py one two three
at the command line:>>> import sys
>>>print(sys.argv)
['demo.py','one','two','three']
The
argparse
module provides a more sophisticated mechanism to process command line arguments. The following script extracts one or more filenames and an optional number of lines to be displayed:Regular Expression
The
re
module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:>>>import re
>>>re.findall(r'bd[a-z]*',
"Follow the blog for more updates")
['Follow','for']
>>>re.sub(r'(\b[a-z]+) \1', r'\1',
'cat in the the hat')
'cat in the hat'
When only simple capabilities are needed, string methods are preferred because they are easier to read and debug:
>>>"Tickets for two".replace('two','us')
'Tickets for us'
Maths
The
math
module gives access to the underlying C library functions for floating-point math:>>>import math
>>>math.cos(math.pi/4)
0.707016
>>>math.log(1024,2)
10
The
random
module provides tools for making random selections and The statistics
module calculates basic statistical properties (the mean, median, variance, etc.) of numeric data:Internet Access
There are a number of modules for accessing the internet and processing internet protocols. Two of the simplest are
urllib.request
for retrieving data from URLs and smtplib
for sending mail:Dates and Times
The
datetime
module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient member extraction for output formatting and manipulation. The module also supports objects that are timezone aware.Data Comprehension
Common data archiving and compression formats are directly supported by modules including: zlib, gzip, bz2, lzma, zipfile and tarfile.
>>> import zlib
>>> 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
No comments