One: module invocation
Two: Time module
Three: System interface module OS and SYS
Four: Several modules of data preservation Json,pickle,xml,configparse
V: Data replication Mobile Module Shutil
Six: Random number randomly
Seven: Log module logging
Eight: Regular expression re
The module is used to split a program, just like a function or class to split a program. Splitting different functions of a program into multiple files can improve code readability. A py file may only complete an algorithm, or complete a socket, which can be called repeatedly. Python uses import to invoke the module, which can be either built-in or customizable, or open-source modules can be used
how to invoke the module:
Module1
defShowstar (num):#the way to count the stars Print("Sky has%s star"%num)classStarsky:#number of stars of the class def __init__(self,num): Self.num=num self.show ()defShow (self):Print("Sky has%s star"%self.num)
Called by
Import Module1 # Importing module fromimport Starsky # Classes in the Import module from import Showstar as show # Import function in module # When executed, the imported things will be parsed A=starsky (12)
If the relationship between Module1 and Module2 is:
| Caller. py
| __init__.py
|
\---Module_dir
module1.py
__init__.py
To invoke the Module1 method:
Import Module_dir.module1 as Mod1 from Import Starskya=starsky (12)Mod1.showstar
The key factor that can be imported is the environment variable, which executes the parent directory of the Python script as an environment variable:
Import sys>>> sys.path[' d:\\visual doc\\pyproject\\ Module Research topic ', ' e:\\ software \\python\\python35.zip ', ' e:\\ software \\python\\DLLs ', ' e:\\ software \\python\\lib ', ' e:\\ software \\python ', ' e:\\ software \\python\\lib\\site-packages ']
Module_dir under the D:\\visual doc\\pyproject\\ module study, so I found
If this is the case:
| module1.py
| __init__.py
|
\---Module_dir
__init__.py
Caller. py
The caller's parent directory Module_dir without Module1 will be an error.
Import syssys.path.append (".. " #sys. Path is a list of environment variables, as long as you add the following: You can represent the parent directory of the Module1 import Module1 as Mod1fromimport Starskya= Starsky (Mod1.showstar) (12)
| __init__.py
| Caller. py
|
+---Module_dir
class1.py
module1.py
__init__.py
Caller calls Module1:import Module_dir.module1
Module1 Call Class1:import Class1
Execution of the caller, Class1 will not be found, because the environment variable is the caller's parent directory, Class1 in this directory can not be found, so in the import should be the Module_dir path to the environment variable
The absolute path to the parent directory of the Module1 __file__.
Import SYS Import ossys.path.append (Os.path.dirname (Os.path.abspath(__file__)))
So: one of the habits is that when you want to invoke another file in the current directory, save the absolute path of the current directory to the environment variable
A little bit more, each time you create a file, write the above code in.
Several important built-in modules:
1, Time module
Familiar with the C language of the time.h, analogy Python This module, are very similar, as a built-in module function, as long as you can use it.
Members |
Parameters/Return |
Description |
Example/return results |
Time.clock () |
|
Return Processor Time |
|
Time.time () |
Return value: Time stamp of the current time |
Returns the timestamp of the current time |
Results: 1471246422.8152277 |
Time.ctime () |
Parameter one: Time stamp Return value: Time string |
Default returns the current time, you can specify a timestamp, return time |
Print (time.ctime (0)) Results: Thu Jan 1 08:00:00 1970 |
Time.gmtime () Time.localtime () |
Parameter one: Time stamp |
LocalTime returns the struct_time of the current time Gmtime returns the time of the current green time zone |
Time.struct_time ( tm_year=1970, Tm_mon=1, Tm_mday=1, Tm_hour=0, Tm_min=0, Tm_sec=0, Tm_wday=3, Tm_yday=1, tm_isdst=0) |
Time.mktime () |
Parameter one: struct_time |
Struct_time Convert to Timestamp |
|
Time.strftime () |
Parameter one: Specify the format Parameter two: Incoming Struct_time Return: Format time string |
Struct_time converted to format time, parameter one and the date of Linux |
2016-08-15 08:02:28 |
Time.strptime () |
Parameter one: string Parameter two: Format return: Struct_time |
|
|
Time.sleep () |
Parameter one: number of seconds |
Specify time to Pause |
Time.sleep (3) Pause for 3 seconds |
Explanation of the time.h of->c language python time
2, operating system-related, OS and SYS modules
The OS module has functions that can execute the operating system commands, there are directory series (directory additions, deletions, moves, duplicates, attributes, soft and hard links), there are path series (current path, superior path, absolute path), many ...
To determine the current operating system:
Os.name windows--> ' nt ' linux/unix--> ' POSIX '
Sys.platform windows--> ' Win32 ' Linux--> ' Linux '
Direct command line operation:
Os.system (command) direct operation
The result of the Os.popen (cmd,mode,bufsize) command operation is returned to the variable, which returns the <os._wrap_close object at 0x000002308955dda0>
Catalogue/File Series:
Os.chdir (path) changes the current directory
Os.listdir (path) lists the files in the current directory
OS.GETCWD () absolute path to the current directory
Os.makedirs (path)/os.removedirs (path) to add/remove directories recursively
Os.remove (file) Delete
Os.rename (old,new) Rename
Path series
Os.environ/sys.path:
Os.environ is the environment variable of the system
Sys.path is the python default path
Os.path.abspath (path) parameter absolute path to path
Os.path.split (path) divides the parameter path into directories and files
Os.path.dirname (path)/os.path.basename (path)
Os.path.isdir (path)/os.path.isfile (file) to determine the path and files
Os.path.join (path,name) path and name merge
Os.path.splitext (file) file is split into file name and extension
SYS.ARGV Receive Run parameters
3, data documents and configuration documents:
Light weight data exchange Xml,json,ini
4,shutil files, folders, compressed package copies, copy file objects
5,logging Log Module
6,re Regular Expressions
Python several important module memos