Module
A. module : A collection of code that implements a feature with one or more. py files, providing reusability of code and coupling between code.
For a complex function, multiple functions may need to be called to complete, and the function can be in a different. py file
, the code collection for these n. py files is called a module
Two. module-to-Library relationship : A library may contain many modules, but a library has at least one module
Types of modules:
- Built-in modules: similar to Sys,os,getpass,socket,time ....
- Custom modules:
- Third-party modules
Three. methods and considerations for importing custom modules :
Note 1: Try to avoid using the IDE to add a module search path
For the same code. Unable to identify module ' MOKUAI2 ' In the absence of pycharm editing environment
Note 2: module search Order of the program
Note 3: Import method
Note 4: Add a new module search path
Four. Python's built-in modules
1.sys: Used to provide actions related to the Python interpreter:
SYS.ARGV command line argument list, the first element is the program itself path Sys.exit (n) exits the program, exit normally (0) sys.version Gets the version information of the Python interpreter Sys.maxint the largest int value Sys.path returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing Sys.platform Returns the operating system platform name Sys.stdin input related sys.stdout output related sys.stderror error related
1.SYS.ARGV: Get the user's input
2. Write the user's input, according to user input, create a corresponding directory
Import OS import sys os.mkdir (sys.argv[1]) testfile = input ("Enter a file name:") Os.mkdir (testfile)
3.sys.stdout (): Percent progress bar
# Make progress bar print something on the monitor standard output import sys import time for I in range: sys.stdout.write (' \ R ') # Every time you empty the original line sys.stdout.write ('%s%%%s '% (int ((i+1)/30*100), i* ' * ')) ' to output%, need to be escaped, escape method is percent "' Sys.stdout.flush () # Empty buffer Time.sleep (0.3)
2. Time-related operations, time has three kinds of means:
- timestamp seconds after January 1, 1970, that is: Time.time ()
- formatted string 2014-11-11 11:11, i.e.: time.strftime ('%y-%m-%d ')
- structured time tuple contains: year, day, Week wait ... time.struct_time i.e.: Time.localtime ()
Import Timetime.sleep (5) ' program stopped 5 Seconds ' ' Print (Time.time ()) ' Timestamp: 1495539499.9574285 ' Print (Time.ctime ()) # string format ' Tue may 19:38:19 ' ' Print (Time.ctime (Time.time ()-86400) # minus the number of seconds in a day, converting the timestamp to a string time ' Mon 22 19:38:19 201 7 "# Convert timestamp to time object Format time_obj = Time.gmtime (Time.time ()) print (Time_obj)" Time.struct_time (tm_year=2017, Tm_mon=5, TM _mday=23, tm_hour=11, tm_min=38, tm_sec=19, Tm_wday=1, tm_yday=143, tm_isdst=0) ' Print (time_obj.tm_year) ' 2017 ' Print (str (time_obj.tm_year) + '-' + str (time_obj.tm_mon) + '-' + str (time_obj.tm_mday)) ' ' 2017-5-23 ' # string format print ("% s-%s-%s%s:%s "% (Time_obj.tm_year, Time_obj.tm_mon, Time_obj.tm_mday, Time_obj.tm_hour, time_obj.tm_min)) ' 2017-5-23 11:38 ' Print ("%d-%f-%d%d:%d"% (Time_obj.tm_year, Time_obj.tm_mon, Time_obj.tm_mday, Time_obj.tm_hour, Time_obj.tm_min) "2017-5.000000-23 11:38%d can only receive integer numbers, but%s can output a time object print for various types of data ' # local time (Time.localtime ( Time.time ()) "Time.struct_time (tm_year=2017, tm_mon=5, tm_mday=23, tm_hour=19, tm_min=38, tm_sec=19, Tm_wday=1, tm_yday=143, tm_isdst=0) ' # Convert time objects to timestamp print (Time.mktime (Time.localtime ())) ' 1495539499.0 "# Converts the time object format to the specified string format print (Time.strftime ("%y-%m-%d%h:%m:%s ", Time.gmtime ())" 2017-05-23 11:38:19 " ' Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) "2017-05-23 19:38:19" # Converts a string format to a time Object format print ( Time.strptime ("2016-01-28", "%y-%m-%d")) "Time.struct_time (tm_year=2016, Tm_mon=1, tm_mday=28, tm_hour=0, tm_min=0 , Tm_sec=0, Tm_wday=3, tm_yday=28, tm_isdst=-1) ' Import Datetimeprint (Datetime.date.today ()) ' 2017-05-23 ' ' Print ( Datetime.date.fromtimestamp (Time.time () -664000000)) "1996-05-08" current_time = Datetime.datetime.now () print ( Current_time) ' 2017-05-23 19:38:20.001431 ' Print (Current_time.timetuple ()) # Returns the time Object format ' Time.struct_time (tm_ year=2017, tm_mon=5, tm_mday=23, tm_hour=19, tm_min=38, tm_sec=20, Tm_wday=1, tm_yday=143, Tm_isdst=-1) ' Print ( Current_time.replace (2014, 12, 12)) # Outputs the current time, the specified value is replaced by ' 2014-12-12 19:38:20.001431 ' str_to_date = DatEtime.datetime.strptime ("21/11/06 16:30", "%d/%m/%y%h:%m") #将字符串转换成日期的字符串格式print (str_to_date) ' 2006-11-21 16:30:00 "# time is used a lot new_date0 = Datetime.datetime.now () + Datetime.timedelta (days=-10) #减10天" "" New_date1 = Dateti Me.datetime.now () + Datetime.timedelta (hours=-10) #减10个小时 "" New_date2 = Datetime.datetime.now () + Datetime.timedelta (seconds=-10) # minus 10 seconds ""
18.python full Stack Road: Modules