One: The introduction of the module:
With a piece of code to achieve a function, modules are divided into: custom modules, built-in modules, open source modules.
1. Time Module
Import time
Print (Time.time ()) #返回当前系统的时间戳
Print (Time.ctime ()) #返回当前系统的时间
Print (Time.ctime (Time.time () -86640)) #将时间戳转化为字符串格式
Print (Time.gmtime (Time.time () -86640)) #将时间戳转化为struct_time格式
Print (Time.localtime (Time.time () -86640)) #将时间戳转化为struct_time格式 and returns local time
Print (Time.mktime (Time.localtime ())) #将struct_time格式转化为时间戳格式
Print (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.gmtime ())) #将struct_time格式转化为指定格式
Print (Time.strptime (' 2016-01-22 ', '%y-%m-%d ')) #将字符串格式转化为struct_time格式
2. DateTime module
Import datetime
Print (Datetime.date.today ()) #输出格式为2016-01-22
Print (Datetime.date.fromtimestamp (Time.time () -86640)) #将时间戳转化为日期格式
Print (Datetime.datetime.now ()) #输出格式为: 2016-01-22 17:22:23.652738
Print (Datetime.datetime.now (). Timetuple ()) #返回struct_time格式
Print (Datetime.datetime.now (). Replace (2011,1,1)) #输出替换时间, and the current time is replaced
Print (Datetime.datetime.now () + datetime.timedelta (days = ten)) #比现在多10天
Print (Datetime.datetime.now () + Datetime.timedelta (days =-)) #比现在少10天
Print (Datetime.datetime.now () + Datetime.timedelta (Hours =-)) #比现在少10小时
Print (Datetime.datetime.now () + datetime.timedelta (seconds =-120)) #比现在少120秒
3. Random Module
Import Random
Print (Random.random ()) #随机生成一个数0.0~1.0
Print (Random.randint (1,10)) generates a random number between #在1-10
Print (Random.randomrange (1,10)) generates a random number between #在1-10, excluding the upper Number 10
For example: randomly generated verification code:
1 ImportRandom2Checkcode ="'3 forIinchRange (0,4):4Rand_num = Random.randrange (0,4)5 ifRand_num = =I:6temp = Random.randint (0,9)7 Else:8temp = Chr (Random.randint (65,90))9Checkcode =Str (temp)Ten Print(Checkcode)
Random
4. OS Module
Provides an interface to invoke the operating system.
Import OS
OS.GETCWD () #获取当前python脚本的工作的目录路径
Os.chdir (' directory ') #改变当前的脚本目录, equivalent to a subdirectory in the CD directory
Os.curdir #返回当前目录的:('. '); I don't know how to use it
Os.pardir () #返回当前目录的父目录:('.. ') I don't know how to use it.
Os.makedirs (' A/b ') #生成多级递归目录
Os.removedirs (') #若目录为空则删除 and returns to the parent directory of the Directory
Os.mkdir (') #创建单级目录, equivalent to mkdir dir
Os.rmdir (") #删除单级目录, if the directory is not empty can not be deleted, will be an error
Os.listdir (') #列出指定目录下的所有文件和子目录, including hidden files, and printed in a list
Os.remove () #删除本级目录中的一个文件
Os.rename (' oldname ', ' NewName ') # renames a file or directory if a named directory or file exists
Os.stat (' directory ') #获取目录或文件的信息
Os.sep #输出操作系统的特定的路径分隔符Windows为 ' \ \ ', Linux as '/'
Os.linesep #输出当前平台使用的行终止符, Windows is ' \ r \ n ', Linux is '/n '
Os.pathsep #输出当前平台用于分割文件路径的字符串, Windows for '; ', Linux as ': '
Os.name #输出字符串表示当前使用的平台; Windows uses: ' NT ', Linux uses: ' POSIX '
Os.system (' Shell command ') #运行shell命令, direct display; using a child shell; The return value has a status of ' 0 '
Os.environ #获取系统的环境变量; basic useless.
Os.popen. (' Shell command ') read () #执行命令的结果以字符串的形式读出来
Os.path.isfile (") #判断是否为一个文件
Os.path.isdir (") #判断是否为一个目录
Note: After 19 is not regular ...
5. SYS module
Import Sys
Sys.argv[0] #表示代码本身的路径
Sys.exit () #退出程序
Sys.version #获取python解释程序的版本信息
Sys.maxint #最大的int值
Sys.path #返回模块的搜索路径, using the value of the PYTHONPATH environment variable when initializing
Sys.platform #返回操作系统平台的名称
Sys.stdout.write (' # ') #输出字符 ' # ', for example: Make progress bar
1 Import SYS 2 Import Time 3 for in range4 sys.stdout.write ('#') 5 sys.stdout.flush ()6 time.sleep (0.3)
Sys.stdout.write
Sys.stdin.readline () #输出带有回车/n this character;
Sys.stdin.readline () [: -1] #输出不带有/n this character
6. Shutil Module
Advanced files, folders, Compression pack processing modules
Import Shutil
Shutil.copyfileobj (fscr,fdst,lenth) #将一个已打开的文件内容copy到另外一个已打开的文件中, you can label the length of copy
Shutil.copyfiel (SRC,DST) #copy文件
Shutil.copymode (SRC,DST) #仅copy权限, content, group, user are not changed
Shutil.copystat (SRC,DST) #copy状态信息, including: mode bits, Atime, mtime, Flags
Shutil.copy (SRC,DST) #copy文件和权限
Shutil.copy2 (SRC,DST) #copy文件和状态信息
Shutil.copytree (Src,dst,symlinks=false,ignore=none) #递归的去copy文件, soft connection does not copy, you can also define some files do not copy
Shutil.rmtree () #递归的去删除目录
Shutil.move (SRC,DST) #递归的去移动目录
Shutil.make_archive (Base_name,format,root_dir)
#base_name: Compress the package name, or it can be a compressed package path, just save the current path when the file name, otherwise save the specified path
#format: Compressed package type; ' Zip, tar, Bztar, Gztar '
#root_dir: Path of the file to be compressed (default current path)
#owner: User, default Current user
#group: Group, default current group
#logger: Used to log logs
For example:
1 ImportShutil2ret = shutil.make_archive ('www','Tar', base_dir='/usr/etc/name')3 #package files under/usr/etc/name in this directory named: www, packaged by: Tar4 5 6 ImportShutil7Ret=shutil.make_archive ('/usr/etc/www','Tar', base_dir='/usr/etc/name')8 #pack the files under/usr/etc/name to/usr/etc/www, packaged as: Tar
make_archive
Shutil Processing of compressed packets is implemented using ZipFile and tarfile two modules.
7, ZipFile and Tarfile modules
11, ZipFile2 Compression:3 ImportZipFile4z = zipfile. ZipFile ('Baoge','W')5Z.write ('A.log')6A.write ('B,log')7 z.close ()8 Unzip:9 ImportZipFileTenA = ZipFile. ZipFile ('Baoge','R') One Z.extractall () A Z.close - -2, Tarfile the Compression: -z = Tarfile.open ('Baoge','W') -Z.add ('/usr/bin/a.log', arcname='A.log') -Z.add ('/usr/bim/b.log', arcname='B.log') + z.close () - Unzip: +z = Tarfile.open ('Baoge','R') AZ.extractall ()#you can set the decompression address atZ.close ()
ZipFile and Tarfile
Python module parsing