宁波python学习_python学习第五天
一 常用模塊
定義:可以實現(xiàn)某種功能的.py結尾的python文件,文件名為test.py,則模塊名為test
導入方法
import ? 模塊名
import ?模塊名1,模塊名2
from 模塊名 import * ? ? ? ? ? 不建議使用這種方法,容易引起模塊中的方法被程序中的同名方法覆蓋的錯誤
from 模塊名 import ?方法名1,方法名2
from 模塊名 import 方法名 as 別名
import本質
import 包名 ? 執(zhí)行包中的__init__.py
import 模塊名 ? 將模塊中所有代碼賦值給以這個模塊名命名的變量
from 模塊名 import 方法名 ? ?把指定方法的代碼賦值給以這個方法名命名的變量
導入優(yōu)化
使用from ? import方式導入模塊方法,加快程序運行速度
模塊分類
標準模塊
time模塊
'''輸出當前時間的時間戳'''
print(time.time())
'''程序停止幾秒'''
# time.sleep(2)
'''時間戳轉換為時間元組形式,以UTC時區(qū)為標準'''
print(time.gmtime())
'''時間戳轉換為時間元組形式,以本地時區(qū)為標準'''
print(time.localtime())
x =time.localtime()
'''提取時間元組中的元素'''
print(x.tm_mday)
'''時間元組轉換成時間戳'''
print(time.mktime(time.localtime()))
'''時間元組轉換成格式化的時間'''
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
'''格式化時間轉換成指定格式的時間元組'''
print(time.strptime(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()),"%Y-%m-%d %H:%M:%S"))
'''時間元組轉換成時間字符串'''
print(time.asctime(time.localtime()))
'''時間戳轉換成時間字符串'''
print(time.ctime(time.time()))
datetime模塊
'''獲取當前時間'''
print(datetime.datetime.now())
'''獲取當前日期幾天前或幾天后的時間,負數(shù)代表幾天前,正數(shù)代表幾天后'''
print(datetime.datetime.now()+datetime.timedelta(-1))
random模塊
'''獲取隨機浮點數(shù),范圍0-1'''
print(random.random())
'''獲取隨機浮點數(shù),范圍自定義'''
print(random.uniform(3,6))
'''獲取隨機整數(shù),范圍自定義'''
print(random.randint(3,8))
'''獲取隨機整數(shù),范圍自定義,范圍左閉右開'''
print(random.randrange(3,8))
'''在傳入序列中獲取一位隨機值'''
print(random.choice([2,5,"a",9]))
'''在傳入序列中取出指定位數(shù)的值,以列表形式返回'''
print(random.sample([2,5,"a",9],2))
'''將傳入序列的順序打亂'''
list =[2,5,"a",9]
random.shuffle(list)
print(list)
import random
checkcode =""
for i in range(4):
current = random.randrange(0,4)
if current == i:
tmp = chr(random.randint(65,90))
else:
tmp = random.randint(0,9)
checkcode += str(tmp)
print(checkcode)
以上代碼實現(xiàn)輸出四位驗證碼的功能
os模塊
'''獲取當前的操作目錄'''
print(os.getcwd())
'''切換工作目錄'''
os.chdir("/root")
'''遞歸的創(chuàng)建目錄'''
# os.makedirs("/root/a/b/c/")
'''遞歸刪除空目錄'''
# os.removedirs("/root/a/b/c/")
'''創(chuàng)建目錄,無法遞歸創(chuàng)建'''
# os.mkdir("/root/a/")
'''刪除指定目錄,無法遞歸刪除'''
# os.rmdir("/root/a/")
'''列出指定目錄下的所有內容'''
print(os.listdir("/root/"))
'''重命名文件或目錄'''
# os.rename("/root/test.txt","/root/3-30.txt")
'''查看文件屬性'''
print(os.stat("/root/3-30.txt"))
'''輸出當前系統(tǒng)的路徑分隔符'''
print(os.sep)
'''輸出當前系統(tǒng)的換行符'''
print(os.linesep)
'''輸出當前系統(tǒng)的文件路徑分隔符'''
print(os.pathsep)
'''輸出當前系統(tǒng)類型'''
print(os.name)
'''執(zhí)行當前系統(tǒng)的操作命令,不保存結果'''
os.system("ls")
'''執(zhí)行當前系統(tǒng)的操作命令,結果保存在內存中,使用read方法獲取'''
print(os.popen("ls").read())
'''輸出系統(tǒng)環(huán)境變量'''
print(os.environ)
'''輸出文件絕對路徑'''
print(os.path.abspath("3-30.txt"))
'''分隔文件目錄路徑和文件名'''
print(os.path.split("/root/3-30.txt"))
'''輸出文件路徑中的目錄部分'''
print(os.path.dirname("/usr/local/bin/a.txt"))
'''輸出文件路徑中的文件名部分'''
print(os.path.basename("/usr/local.bin/a.txt"))
'''判斷路徑是否存在'''
print(os.path.exists("/usr/local/a.txt"))
'''判斷輸入的是否為絕對路徑'''
print(os.path.isabs("3-20.txt"))
'''判斷輸入的是否為一個存在的文件'''
print(os.path.isfile("3-20.txt"))
'''判斷輸入的是否為一個存在的目錄'''
print(os.path.isdir("/usr/"))
'''將輸入的多個路徑組合返回,最后一個絕對路徑前的內容會被忽略'''
print(os.path.join("a","/usr","local","a.txt"))
'''輸出文件或目錄最后的存取時間'''
print(os.path.getatime(__file__))
'''輸出文件或目錄最后的修改時間'''
print(os.path.getmtime(__file__))
sys模塊
sys.argv
sys.exit() ?退出程序,正常推出為exit(0)
sys.version 獲取python解釋器版本信息
sys.maxint 最大Int值
sys.path 返回python環(huán)境變量值
sys.platform 返回操作系統(tǒng)平臺名稱
sys.stdout.write()
val = sys.stdin.readline()[:-1]
shutil模塊
shutil.copyfileobj() ?傳入兩個文件對象,復制源文件內容到目標文件
shutil.copyfile() 輸入源文件名,目標文件名進行復制
shutil.copymode() ? 將源文件權限模式復制給目標文件
shutil.copystat() ?將源文件屬性復制給目標文件
shutil.copy() ? ?復制文件和權限
shutil.copy2()?復制文件和權限和狀態(tài)信息
shutil.copytree() 遞歸的復制文件
shutil.rmtree() 遞歸刪除目錄
shutil.move() 遞歸移動文件
shutil.make_archive("壓縮包名或路徑","壓縮類型","要壓縮文件的路徑")
shelve模塊
通過key-value將內存數(shù)據(jù)通過文件持久化
import shelve
d =shelve.open("shelve_test")
name =[1,2,3,4]
info = {"name":"lf","age":23}
d["name"] =name ?列表持久化
d["info"] =info ?字典持久化
d.close()
print(d.get("info")) ?取出持久化文件內容
xml模塊
import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
# 遍歷xml文檔
for child in root:
print(child.tag, child.attrib) ?打印xml標簽,屬性
for i in child:
print(i.tag, i.text) ?打印標簽,文本
# 只遍歷year 節(jié)點
for node in root.iter('year'):
print(node.tag, node.text)
#修改
for node in root.iter('year'):
new_year = int(node.text) +1
node.text =str(new_year)
node.set("updated_by","Alex")
tree.write("xmltest.xml")
#刪除
for country in root.findall('country'):
rank =int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write("output.xml")
創(chuàng)建
import?xml.etree.ElementTree as ET
總結
以上是生活随笔為你收集整理的宁波python学习_python学习第五天的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QPS、TPS、并发用户数、吞吐量的关系
- 下一篇: hiveserver2 mysql_Hi