1. Definition and import of packages and modules
1.1. What are python packages and modules
- A package is a folder. A package can also have a package, that is, a subfolder
- python file modules
1.2. ID card of package
__ init__.py is a file that must exist in every python package. There can be no content in this file
1.3. How to create a package
- There should be a theme, clear functions and easy to use
- Clear hierarchy and clear call
- The identity authentication document of the package shall be included in the document, i.e__ init__.py file to prove that the folder is a package
1.4. Import of packages
Although there are sub packages and sub files in the illustrated animal package, import animal can only get the current package__ init__. Functions in py; When importing a specific file, such as import test1 Py can only take the functions in the current module (current file), and the functions in the same level animal package cannot be accessed and used
1.5. Module import
2. Third party package
- Alibaba cloud http://mirrors.aliyun.com/pypi/simple/
- Watercress http://pypi.douban.com/simple/
- Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/
- University of science and technology of China http://pypi.mirrors.ustc.edu.cn/simple/
- Huazhong University of science and technology http://pypi.hustunique.com/
- Use format: if you install IPython, the version is 19.0 1 package PIP install - I http://mirrors.aliyun.com/pypi/simple/ ipython==19.0. one
3. datetime and time of Python
- datetime and time are two time packages commonly used in python
- datetime is often used to process dates
- Time is often used to process time, timing, etc
3.1.datetime
- date and time combination
- Get current time
- Get interval
- Convert time object to time string
- Convert string to time type
3.1. 1. Common functions of datetime package
Get current time
Get interval
1 # coding:utf-8 2 3 from datetime import datetime 4 from datetime import timedelta 5 6 now=datetime.now() 7 print(now,type(now)) #2021-12-27 16:19:18.586413 <class 'datetime.datetime'> 8 9 three_days=timedelta(days=3) 10 print(type(three_days)) #<class 'datetime.timedelta'> 11 12 after_three_day=now+three_days 13 print(after_three_day) #2021-12-30 16:19:18.586413
Time object to string
1 # coding:utf-8 2 3 from datetime import datetime 4 5 now=datetime.now() 6 now_str=now.strftime('%Y-%m-%d %H:%M:%S') 7 print(now_str,type(now_str)) #2021-12-27 16:28:06 <class 'str'> Date string cannot realize the addition and subtraction of date. It must be converted to date object type to realize the addition and subtraction of date
Time string to time type
1 # coding:utf-8 2 3 from datetime import datetime 4 from datetime import timedelta 5 6 now=datetime.now() 7 now_str=now.strftime('%Y-%m-%d %H:%M:%S') 8 print(now_str,type(now_str)) #2021-12-27 16:37:11 <class 'str'> 9 10 now_obj=datetime.strptime(now_str,'%Y-%m-%d %H:%M:%S') 11 print(now_obj,type(now_obj)) #2021-12-27 16:37:11 <class 'datetime.datetime'>,When converting to an object, the following format must match the format of the string 12 13 three_days=timedelta(days=3) 14 after_three_day=now_obj+three_days 15 print(after_three_day,type(after_three_day)) #2021-12-30 16:37:11 <class 'datetime.datetime'>
3.1. 2. Common time formatting symbols in Python
1 # coding:utf-8 2 3 from datetime import datetime 4 5 now=datetime.now() 6 now_str=now.strftime('%Y-%m-%d %H:%M:%S %p %j %U %A') 7 print(now_str,type(now_str)) #2021-12-27 16:45:31 PM 361 52 Monday <class 'str'>
3.2.time
3.2. 1. Time stamp
- Total milliseconds (seconds) since 00:00:00 on January 1, 1970
- Use timestamp for timestamp
- The timestamp is of type float
3.2. 2 understand the time module and common methods of python
Generate timestamp function time
1 # coding:utf-8 2 import time 3 4 now=time.time() 5 print(now,type(now)) #1640595949.671707 <class 'float'>
Get local time function localtime
timestamp does not represent the current time
1 # coding:utf-8 2 import time 3 4 now=time.time() 5 time_obj=time.localtime(now) 6 print(time_obj,type(time_obj)) #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=17, tm_min=6, tm_sec=43, tm_wday=0, tm_yday=361, tm_isdst=0) <class 'time.struct_time'>
Introduction to corresponding fields of localtime
1 # coding:utf-8 2 import time 3 4 now=time.localtime() 5 print(now) #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=17, tm_min=1, tm_sec=12, tm_wday=0, tm_yday=361, tm_isdst=0)
Pause function sleep
1 # coding:utf-8 2 import time 3 4 for i in range(10): 5 print(i) 6 time.sleep(1)
strftime and strptime in time
1 # coding:utf-8 2 import time 3 4 now=time.time() 5 print(now,type(now)) #1640596914.3263566 <class 'float'> 6 7 #now_str=time.strftime('%Y-%m-%d %H:%M:%S',now) #TypeError: Tuple or struct_ The time argument required error is reported because the now timestamp is a floating point type, not time Time type corresponding to Localtime 8 print(type(time.localtime())) #<class 'time.struct_time'> 9 now_str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) 10 print(now_str,type(now_str)) #2021-12-27 17:21:54 <class 'str'>
1 # coding:utf-8 2 import time 3 4 now=time.time() 5 now_str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) 6 now_obj=time.strptime(now_str,'%Y-%m-%d %H:%M:%S') 7 print(now_obj,type(now_obj)) #time.struct_time(tm_year=2021, tm_mon=12, tm_mday=27, tm_hour=18, tm_min=5, tm_sec=40, tm_wday=0, tm_yday=361, tm_isdst=-1) <class 'time.struct_time'>
3.2.3datetime to timestamp and datetime timestamp to time object
datetime to timestamp
1 # coding:utf-8 2 3 from datetime import datetime 4 5 now=datetime.now() 6 now_stamp=datetime.timestamp(now) 7 print(now_stamp,type(now_stamp)) #1640600288.843319 <class 'float'>
datetime timestamp and time timestamp to time object
1 # coding:utf-8 2 3 from datetime import datetime 4 import time 5 6 now=datetime.now() 7 now_stamp=datetime.timestamp(now) 8 now_stamp_obj=datetime.fromtimestamp(now_stamp) 9 print(now_stamp_obj,type(now_stamp_obj)) #2021-12-27 18:56:15.849622 <class 'datetime.datetime'> 10 11 now_time=time.time() 12 now_time_obj=datetime.fromtimestamp(now_time) 13 print(now_time_obj,type(now_time_obj)) #2021-12-27 18:56:15.849623 <class 'datetime.datetime'>
4.Python built-in library os and sys module
4.1.os module
4.1. 1. Introduction to OS file and directory functions
1 # coding:utf-8 2 3 import os 4 5 current_path=os.getcwd() 6 print(current_path) 7 8 new_path='%s/test1/test2' % current_path 9 os.makedirs(new_path) 10 11 os.removedirs('test1/test2') 12 os.rename('test1','test') 13 data=os.listdir(current_path) 14 print(data)
4.1. 2.os. Introduction to common functions of path module
4.2.sys module