Module: essentially a file at the end of. py. Logically organize python code.
Package: in essence, it is a directory with "init". Py file to logically organize modules.
Module classification:
1. Standard library (built-in module)
2. Open source library (third-party library)
3. Custom module
Module import method:
1.import + module name
2.from......import......
The essence of import module is to explain and execute the imported. py file once
The essence of the import package is to execute the "init. Py" file once.
1. Time module (time and datetime)
1. Timestamp 2. Format ﹣ time 3. Struct ﹣ time
1 import time 2 print(time.asctime()) #Accept a tuple to a fixed form of string time Thu Jan 10 18:33:34 2019 3 print(time.altzone) #Calculation and utc Seconds of time difference 4 print(time.ctime()) #Accept a timestamp and convert it to a fixed form of string time Thu Jan 10 18:33:34 2019 5 print(time.gmtime()) #Converting time in the form of time stamp to structured time utc time 6 print(time.localtime()) #Convert time stamp form time to structured time local time 7 print(time.mktime()) #Convert structured time to timestamp time 8 print(time.strftime("%Y-%m-%d %H:%M:%S","Structured time"))#Convert structured time to string time 9 print(time.strptime("2016-12-10 10:12:13","%Y-%m-%d %H:%M:%S"))#Convert string time to structured time 10 print(time.sleep()) #Time expand
Transformation of time
1 import datetime 2 import time 3 print(datetime.datetime.now()) #Current time fixed format 4 print(datetime.date.fromtimestamp(time.time())) #Turn timestamps into 2019-1-10 5 print(datetime.datetime.now()+datetime.timedelta(hours=+3))#Current time plus 3 hours, 6 print(datetime.datetime.now()+datetime.timedelta(days=-3))#Current time minus 3 days 7 print(datetime.datetime.now().replace(month=2,day=3)) #Time replacement
2. random module
1 import random 2 print(random.choice([1,2,3])) #Randomly select a number in the list 3 print(random.random()) #(0,1)Random floating point number 4 print(random.randint(1,3)) #[1,3]Random integer between 5 print(random.randrange(1,3) #Random integer between (1,3) 6 print(random.seed(20)) #Generate random seeds 7 print(random.sample(range(10),3)) #From (0,9)Randomly select 3 numbers between 8 l = [1,2,3] 9 random.shuffle(l) #Random disruption 10 print(l) 11 print(random.uniform(1,3)) #(1,3)Random floating point number between
1 #Random generation of verification code 2 import random 3 def check_code(num): 4 codes = "" 5 for i in range(num): 6 number = random.randint(0,9) 7 alpha = chr(random.randint(97,122)) 8 code = random.choice([number,alpha]) 9 codes += str(code) 10 return codes 11 l = check_code(5) 12 print(l)
1 import sys 2 print(sys.path) #Return to the search path of the current module 3 print(sys.argv) #Command line parameter, the first parameter is the path of the file itself 4 # sys.exit() #Exit program, exit() normally 5 print(sys.platform) #Return operating platform name 6 print(sys.version) #Return Python Edition
2.os module
1 import os 2 print(os.getcwd()) #Current file directory 3 os.mkdir("11") #Create 11 directories 4 os.makedirs("11/22" ) #Create a multi-level directory 5 print(os.listdir("G:\Echizen\PycharmProjects\one\learn3")) #List all files or subdirectories in this directory 6 os.rename("11/22",'11/33') #directories renaming 7 os.removedirs("11/33") #Delete multi level directory 8 print(os.stat("G:\Echizen\PycharmProjects\one\learn3\day1.py")) #Get file directory information 9 print(os.environ) #Get file environment 10 print(os.path.abspath("G:\Echizen\PycharmProjects\one\learn3\day1.py")) #Get file absolute path 11 print(os.path.split("G:\Echizen\PycharmProjects\one\learn3\day1.py")) #Split the path into directories and files, and return in tuples 12 print(os.path.dirname("G:\Echizen\PycharmProjects\one\learn3\day1.py"))#Get path directory name,Namely split First after split 13 print(os.path.basename("G:\Echizen\PycharmProjects\one\learn3\day1.py"))#Get the path file, that is split The second after 14 print(os.path.exists("G:\Echizen\PycharmProjects\one\learn3\day1.py")) #Determine whether the file exists 15 print(os.path.isabs("G:\Echizen\PycharmProjects\one\learn3\day1.py")) #Determine whether it is an absolute path 16 print(os.path.isfile("G:\Echizen\PycharmProjects\one\learn3\day1.py")) #Determine whether it is a file 17 print(os.path.isdir("G:\Echizen\PycharmProjects\one\learn3"))#Determine whether it is a directory 18 print(os.path.join("G:\Echizen\PycharmProjects\one\learn3","day3")) #Connect 19 print(os.path.getatime("G:\Echizen\PycharmProjects\one\learn3\day2.py")) #Get the last storage time of a file or directory 20 print(os.path.getmtime("G:\Echizen\PycharmProjects\one\learn3\day2.py")
#Import operations between different packages import sys,os print(sys.path) x = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(x) print(sys.path) import ML