爭取寫完,持續更新
文章目錄
- 一.sys
- 1.platfrom方法識別作業系統
- 2.argv方法獲取命令列引數
- 3.exit方法退出程式
- 4.path方法獲取模塊搜索路徑
- 5.modules方法查找已匯入的模塊
- 二.os
- 1.dirname方法獲取當前路徑
- 2.getcwd方法獲取當前路徑,chdir方法切換路徑
- 3.rename方法可以重命名檔案
- 4.exits方法可以查看檔案是否存在
- 5.isfile方法判斷是否是一個檔案
- 6.isdir方法判斷路徑是否是一個目錄
- 7.environ方法獲取系統變數
- 8.mkdir方法創建單層目錄
- 9.mkdirs方法創建多層目錄
- 三.math
- 1.常量圓周率和自然常數e
- 2.ceil函式向上取整,floor函式向下取整
- 3.pow函式為指數運算
- 4.log函式為對數運算,默認底數為e,第二個引數可以設定底數
- 5.sqrt函式為平方根計算
- 6.三角函式計算
- 7.角度與弧度轉化
- 四.random
- 1.random方法用于產生0到1之間的隨機浮點數
- 2.randint方法產生指定區間的亂數
- 3.choice方法獲取序列中的一個亂數
- 4.shuffle方法將序列打亂
- 五.time
- 1.time函式用于回傳當前的時間戳(格林尼治時間起至現在的總秒數)
- 2.localtime函式用于將時間戳格式轉化為本地時間,回傳(struct_time)物件
- 3.mktime函式接收struct_time回傳用秒數表示時間的浮點數
- 4.gmtime函式可以將時間戳轉化為0時區的struct_time
- 5.asctime函式可以接收struct_time回傳可讀的時間形式
- 6.ctime函式可以把時間戳轉化為可讀形式
- 7.sleep函式推遲呼叫執行緒的運行,引數為秒
- 8.strftime函式接收時間元組,回傳可讀的當地時間
- 9.strptime函式可以將時間字符決議為時間元組
- 六.datetime
- 1.date物件
- 2.time物件
- 3.datetime物件
- 4.timedelta物件
- 5.tzinfo物件
- 七.calendar
- 1.calendar.isleap用于判斷閏年
- 2.canlender.leapdays用于回傳兩個年份之間閏年的總數
- 3.canlender.month方法用于制作日歷
- 4.calendar.monthcalender方法回傳一個單層嵌套串列,每個串列里面裝載一個星期
- 5.calendar.monthrange方法回傳兩個整陣列成的元組,第一個數表示該月的第一天是星期幾,第二個數表示改月的天數
- 6.calendar.weekday方法回傳給定日期的星期碼
- 7.calendar.calendar回傳整年月歷
- 八.smtplib+email
- 1.smtp.sendmail(sender,recever,message.as_string())方法用于發送郵件,引數分別為郵件發送者地址,接收者地址,發送方式(一般使用字串)
- 2.Header方法用于構造message物件
- 3.MIMEText用于構造郵件內容
- 4.smtp.login(sender,password)方法用登錄
- 5.MIMEText的第二個引數改成"html"可以發送html格式的電子郵件
- 6.MIMEMultipart物件,attach方法可以添加附件
- 九.IO
- 十.threading
- 十一.queue
- 十二.re
- 十三.smtplib
- 十四.pickle
- 十五.hashlib
一.sys
1.platfrom方法識別作業系統
import sys
print(sys.platform)
win32
2.argv方法獲取命令列引數
import sys
print(sys.argv)
[‘D:/Code/python/Draft/1.py’]
3.exit方法退出程式
import sys
for i in range(5):
print(i)
if i > 2:
sys.exit(1)
0
1
2
3
4.path方法獲取模塊搜索路徑
import sys
for path in sys.path:
print(path)
D:\Develop\python
C:\Users\yh\AppData\Roaming\Python\Python39\site-packages
D:\Develop\python\lib\site-packages
D:\Develop\python\lib\site-packages\django-3.0.11-py3.9.egg
D:\Develop\python\lib\site-packages\asgiref-3.3.1-py3.9.egg
D:\Develop\python\lib\site-packages\pip-20.3.3-py3.9.egg
D:\App\PyCharm 2020.3.2\plugins\python\helpers\pycharm_matplotlib_backend
5.modules方法查找已匯入的模塊
import sys
print(sys.modules.keys())
D:\Develop\python\python.exe D:/Code/python/Draft/1.py
dict_keys([‘sys’, ‘builtins’, ‘_frozen_importlib’, ‘_imp’, ‘_thread’, ‘_warnings’, ‘_weakref’, ‘_frozen_importlib_external’, ‘nt’, ‘_io’, ‘marshal’, ‘winreg’, ‘time’, ‘zipimport’, ‘_codecs’, ‘codecs’, ‘encodings.aliases’, ‘encodings’, ‘encodings.utf_8’, ‘_signal’, ‘encodings.latin_1’, ‘_abc’, ‘abc’, ‘io’, ‘main’, ‘_stat’, ‘stat’, ‘_collections_abc’, ‘genericpath’, ‘ntpath’, ‘os.path’, ‘os’, ‘_sitebuiltins’, ‘_locale’, ‘_bootlocale’, ‘_codecs_cn’, ‘_multibytecodec’, ‘encodings.gbk’, ‘_heapq’, ‘heapq’, ‘itertools’, ‘keyword’, ‘_operator’, ‘operator’, ‘reprlib’, ‘_collections’, ‘collections’, ‘types’, ‘_functools’, ‘functools’, ‘enum’, ‘_sre’, ‘sre_constants’, ‘sre_parse’, ‘sre_compile’, ‘copyreg’, ‘re’, ‘token’, ‘tokenize’, ‘linecache’, ‘traceback’, ‘sitecustomize’, ‘site’])
Process finished with exit code 0
二.os
1.dirname方法獲取當前路徑
import os
print(os.path.dirname(__file__))
D:\Code\python\Draft
2.getcwd方法獲取當前路徑,chdir方法切換路徑
import os
print(os.getcwd())
os.chdir("D:\\")
print(os.getcwd())
D:\Code\python\Draft
D:\
3.rename方法可以重命名檔案
import os
os.rename("a.txt", "b.txt")
4.exits方法可以查看檔案是否存在
import os
print(os.path.exists('a.txt'))
False
5.isfile方法判斷是否是一個檔案
import os
print(os.path.isfile('b.txt'))
True
6.isdir方法判斷路徑是否是一個目錄
import os
print(os.path.isdir("c:\\"))
True
7.environ方法獲取系統變數
import os
for k, v in os.environ.items():
print(k, "=>", v)
ALLUSERSPROFILE => C:\ProgramData
APPDATA => C:\Users\yh\AppData\Roaming
COMMONPROGRAMFILES => C:\Program Files\Common Files
COMMONPROGRAMFILES(X86) => C:\Program Files (x86)\Common Files
COMMONPROGRAMW6432 => C:\Program Files\Common Files
COMPUTERNAME => LAPTOP-J8VS29QD
COMSPEC => C:\WINDOWS\system32\cmd.exe
DRIVERDATA => C:\Windows\System32\Drivers\DriverData
FPS_BROWSER_APP_PROFILE_STRING => Internet Explorer
FPS_BROWSER_USER_PROFILE_STRING => Default
HOMEDRIVE => C:
HOMEPATH => \Users\yh
IDEA_INITIAL_DIRECTORY => C:\Users\yh\Desktop
LOCALAPPDATA => C:\Users\yh\AppData\Local
LOGONSERVER => \LAPTOP-J8VS29QD
NUMBER_OF_PROCESSORS => 12
ONEDRIVE => C:\Users\yh\OneDrive
ONEDRIVECONSUMER => C:\Users\yh\OneDrive
OS => Windows_NT
PATH => D:\App\VMware\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\Intel\Intel? Management Engine Components\DAL;C:\Program Files\Intel\Intel? Management Engine Components\DAL;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;D:\App\Git\cmd;D:\App\Xshell;D:\App\Node;D:\Develop\jdk\bin;D:\Develop\mingw64\bin;D:\Develop\python\Lib\site-packages\Django-3.0.11-py3.9.egg\django;D:\Develop\python\Scripts;D:\Develop\python\Scripts;D:\Develop\python;C:\Users\yh\AppData\Local\Microsoft\WindowsApps;;D:\App\Microsoft VS Code\bin;C:\Users\yh\AppData\Roaming\npm;D:\App\PyCharm 2020.3.2\bin;
PATHEXT => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE => AMD64
PROCESSOR_IDENTIFIER => Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
PROCESSOR_LEVEL => 6
PROCESSOR_REVISION => 9e0d
PROGRAMDATA => C:\ProgramData
PROGRAMFILES => C:\Program Files
PROGRAMFILES(X86) => C:\Program Files (x86)
PROGRAMW6432 => C:\Program Files
PSMODULEPATH => C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PUBLIC => C:\Users\Public
PYCHARM => D:\App\PyCharm 2020.3.2\bin;
PYCHARM_DISPLAY_PORT => 63342
PYCHARM_HOSTED => 1
PYTHONIOENCODING => UTF-8
PYTHONPATH => D:\Code\python;D:\Code\python\Stdent;D:\App\PyCharm 2020.3.2\plugins\python\helpers\pycharm_matplotlib_backend;D:\App\PyCharm 2020.3.2\plugins\python\helpers\pycharm_display
PYTHONUNBUFFERED => 1
SESSIONNAME => Console
SYSTEMDRIVE => C:
SYSTEMROOT => C:\WINDOWS
TEMP => C:\Users\yh\AppData\Local\Temp
TMP => C:\Users\yh\AppData\Local\Temp
USERDOMAIN => LAPTOP-J8VS29QD
USERDOMAIN_ROAMINGPROFILE => LAPTOP-J8VS29QD
USERNAME => yh
USERPROFILE => C:\Users\yh
WINDIR => C:\WINDOWS
8.mkdir方法創建單層目錄
9.mkdirs方法創建多層目錄
三.math
1.常量圓周率和自然常數e
import math
print(math.pi)
print(math.e)
3.141592653589793
2.718281828459045
2.ceil函式向上取整,floor函式向下取整
import math
print(math.ceil(math.pi))
print(math.floor(math.pi))
4
3
3.pow函式為指數運算
import math
print(math.pow(2, 3))
8.0
4.log函式為對數運算,默認底數為e,第二個引數可以設定底數
import math
print(math.log(3))
print(math.log(100, 10))
1.0986122886681098
2.0
5.sqrt函式為平方根計算
import math
print(math.sqrt(4))
2.0
6.三角函式計算
import math
print(math.sin(1))
print(math.cos(math.pi))
0.8414709848078965
-1.0
7.角度與弧度轉化
import math
print(math.degrees(math.pi))
print(math.radians(90))
180.0
1.5707963267948966
四.random
1.random方法用于產生0到1之間的隨機浮點數
import random
print(random.random())
0.6666132029039157
2.randint方法產生指定區間的亂數
import random
print(random.randint(1, 100))
58
3.choice方法獲取序列中的一個亂數
import random
a = (1, 5, 8, 9, 6, 7, 4)
print(random.choice(a))
1
4.shuffle方法將序列打亂
import random
a = [1, 5, 8, 9, 6, 7, 4]
random.shuffle(a)
print(a)
[7, 1, 9, 6, 4, 5, 8]
五.time
1.time函式用于回傳當前的時間戳(格林尼治時間起至現在的總秒數)
import time
now=time.time()
print(now)
1611303060.773287
2.localtime函式用于將時間戳格式轉化為本地時間,回傳(struct_time)物件
import time
now=time.localtime()
print(now)
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=16, tm_min=14, tm_sec=30, tm_wday=4, tm_yday=22, tm_isdst=0)
3.mktime函式接收struct_time回傳用秒數表示時間的浮點數
import time
now=time.localtime()
print(now)
s=time.mktime(now)
print(s)
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=16, tm_min=21, tm_sec=1, tm_wday=4, tm_yday=22, tm_isdst=0)
1611303661.0
4.gmtime函式可以將時間戳轉化為0時區的struct_time
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)
1611303823.960081
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=8, tm_min=23, tm_sec=43, tm_wday=4, tm_yday=22, tm_isdst=0)
5.asctime函式可以接收struct_time回傳可讀的時間形式
import time
now=time.time()
print(now)
s=time.gmtime(now)
print(s)
new=time.asctime(s)
print(new)
1611303959.5288363
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=22, tm_hour=8, tm_min=25, tm_sec=59, tm_wday=4, tm_yday=22, tm_isdst=0)
Fri Jan 22 08:25:59 2021
6.ctime函式可以把時間戳轉化為可讀形式
import time
now=time.time()
print(now)
s=time.ctime(now)
print(s)
1611304057.0304146
Fri Jan 22 16:27:37 2021
7.sleep函式推遲呼叫執行緒的運行,引數為秒
import time
print("start:",time.ctime())
time.sleep(9)
print("end:",time.ctime())
start: Fri Jan 22 16:29:58 2021
end: Fri Jan 22 16:30:07 2021
8.strftime函式接收時間元組,回傳可讀的當地時間
9.strptime函式可以將時間字符決議為時間元組
六.datetime
1.date物件
import datetime
print(datetime.MAXYEAR) #支持的最大年份
print(datetime.MINYEAR) #支持的最小年份
print(datetime.date.today()) #today回傳當天日期
print(datetime.date.today().weekday()) #weekday放回當天的星期
print(datetime.date.today().isoformat()) #回傳IOS格式
9999
1
2021-01-22
4
2021-01-22
2.time物件
import datetime
print(datetime.time()) #默認時間
print(datetime.time.max) #支持的最大時間
print(datetime.time.min) #支持的最小時間
00:00:00
23:59:59.999999
00:00:00
3.datetime物件
datetime=date+time,同時回傳日期和時間
import datetime
today=datetime.datetime.today()
print(today)
print(datetime.datetime.now()) #與today用法相同
print(datetime.datetime.utcnow()) #0時區時間
now=datetime.datetime.now()
print(now.date()) #date物件回傳日期
print(now.time()) #time物件回傳時間
2021-01-22 18:22:42.380595
2021-01-22 18:22:42.382589
2021-01-22 10:22:42.382589
2021-01-22
18:22:42.382589
4.timedelta物件
表示時間差
import datetime
dt1=datetime.datetime.now()
dt2=datetime.timedelta(weeks=2)+dt1
print(dt1)
print(dt2)
print(dt1-dt2)
2021-01-22 18:34:31.429461
2021-02-05 18:34:31.429461
-14 days, 0:00:00
5.tzinfo物件
七.calendar
1.calendar.isleap用于判斷閏年
import calendar
print(calendar.isleap(2000))
print(calendar.isleap(2018))
True
False
2.canlender.leapdays用于回傳兩個年份之間閏年的總數
import calendar
print(calendar.leapdays(1900,2000))
24
3.canlender.month方法用于制作日歷
四個引數:theyear,themonth,w(字符間距),l(行間距)
import calendar
print(calendar.month(2021,1))

4.calendar.monthcalender方法回傳一個單層嵌套串列,每個串列里面裝載一個星期
import calendar
print(calendar.monthcalendar(2021,1))
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]
5.calendar.monthrange方法回傳兩個整陣列成的元組,第一個數表示該月的第一天是星期幾,第二個數表示改月的天數
import calendar
print(calendar.monthrange(2021,1))
(4, 31)
6.calendar.weekday方法回傳給定日期的星期碼
import calendar
print(calendar.weekday(2021,1,1))
4
7.calendar.calendar回傳整年月歷
import calendar
print(calendar.calendar(2021))

八.smtplib+email
1.smtp.sendmail(sender,recever,message.as_string())方法用于發送郵件,引數分別為郵件發送者地址,接收者地址,發送方式(一般使用字串)
2.Header方法用于構造message物件
3.MIMEText用于構造郵件內容
4.smtp.login(sender,password)方法用登錄
from email import message
from email import header
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender="10791894@qq.com"
password="郵箱授權碼"
recever=["10791894@qq.com",]
message=MIMEText("郵件內容","plain","utf-8")
message["From"]=Header("發件人顯示的名字","utf-8")
message["To"]=Header("yh","utf-8")
message["Subject"]="text"
try:
smtp=smtplib.SMTP_SSL("smtp.qq.com")
smtp.login(sender,password)
smtp.sendmail(sender,recever,message.as_string())
print("郵件已經發送")
except smtplib.SMTPException as e:
print("出錯",e)
郵件已經發送
5.MIMEText的第二個引數改成"html"可以發送html格式的電子郵件
from email import message
from email import header
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender="10791894@qq.com"
password="郵箱授權碼"
recever=["10791894@qq.com",]
mail_html="""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<a href="http://www.baidu.com">百度一下</a>
</body>
</html>
"""
message=MIMEText(mail_html,"html","utf-8")
message["From"]=Header("發件人顯示的名字","utf-8")
message["To"]=Header("yh","utf-8")
message["Subject"]="text"
try:
smtp=smtplib.SMTP_SSL("smtp.qq.com")
smtp.login(sender,password)
smtp.sendmail(sender,recever,message.as_string())
print("郵件已經發送")
except smtplib.SMTPException as e:
print("出錯",e)
郵件已經發送
6.MIMEMultipart物件,attach方法可以添加附件
from email import message
from email import header
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
sender="10791894@qq.com"
password="郵箱授權碼"
recever=["10791894@qq.com",]
mail_html="""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<a href="http://www.baidu.com">百度一下</a>
</body>
</html>
"""
message=MIMEMultipart() #指定訊息使用復合型
message["From"]=Header("發件人顯示的名字","utf-8")
message["To"]=Header("yh","utf-8")
message["Subject"]="text"
f=open("test.txt","w",encoding="utf-8")
f.write("test")
f.close()
message.attach(MIMEText(mail_html,"html","utf-8"))
attach_file=MIMEText(open("test.txt",encoding="utf-8").read(),"base64","utf-8")
attach_file["Content-Disposition"]='attachment;filename="mail.py"'
message.attach(attach_file)
try:
smtp=smtplib.SMTP_SSL("smtp.qq.com")
smtp.login(sender,password)
smtp.sendmail(sender,recever,message.as_string())
print("郵件已經發送")
except smtplib.SMTPException as e:
print("出錯",e)
郵件已經發送
九.IO
十.threading
十一.queue
十二.re
十三.smtplib
十四.pickle
https://blog.csdn.net/qq_50216270/article/details/112985808
十五.hashlib
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/251780.html
標籤:python
上一篇:Python爬蟲自學系列(五)
下一篇:MySQL語法學習筆記



