python 中幾種常用的模塊
先在pycharm中建立一個檔案夾,在檔案夾里新建一個檔案
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-DLVW09T6-1608970868392)(C:\Users\42079\Desktop\模塊.png)]
下面關于模塊的內容我都在上述檔案夾內進行的:
1. os模塊:這個是呼叫作業系統的方法,使用方法為:import os
-
查看系統的一些屬性:
import os print(os.name) # 作業系統名稱 Windows nt 非Windows posix print(os.sep) # 路徑分隔符 Windows \ 其他 / -
使用 os.path 方法獲取檔案的路徑
import os # 使用 os.path 方法獲取檔案的路徑 # 001.獲取檔案的絕對路徑 使用abspath方法 print(os.path.abspath("04_模塊匯入.py")) # 運行結果:D:\mypycharm\pythonProject\千峰培訓\day11module1\04_模塊匯入.py # 002判斷是否是檔案 False print(os.path.isdir("")) # 運行結果: False # 003.判斷檔案是否存在如果存在回傳True 否則回傳False print(os.path.exists("mydir")) # True -
獲取檔案名的后綴
import os files = "2020.12.22.test.py" print(files.rpartition(".")[-1]) print(os.path.splitext(files)[-1]) # 運行結果: # 獲取檔案的后綴名 py # 獲取檔案的后綴名 .py -
獲取當前檔案的目錄
import os print(os.getcwd()) # 運行結果: # D:\mypycharm\pythonProject\千峰培訓\day11module1 -
切換路徑
import os os.chdir("mydir") print(os.getcwd()) # D:\mypycharm\pythonProject\千峰培訓\day11module1\mydir -
修改路徑的名字:
import os os.rename("66.py","../99.py") -
洗掉檔案和空檔案夾:
import os # 001.洗掉檔案 os.remove("../99.py") # 002.洗掉空檔案夾 os.rmdir("../mydir") os.removedirs("mydir") -
創建檔案夾
import os os.mkdir("mydir") -
列出檔案夾里的檔案
import os # 001.列出指定目錄里所有的子目錄和檔案 print(os.listdir("D:\mypycharm\pythonProject")) # 002.默認當前目錄里的 子目錄和檔案 print(os.listdir()) # 運行結果: # ['.idea', '千峰培訓', '學校實習'] # ['03_module.py', '04_模塊匯入.py', '05_os.py', '2020.12.22.tests.py', 'a01_module1.py', 'a02_module2.py', '__pycache__'] -
獲取環境變數
import os print(os.environ) print(os.environ["PATH"]) -
os案例:生成一個隨機的檔案名
import os import string # 字串模塊 import random files = "test.jpg" # 01.獲取檔案的后綴 surffix = os.path.splitext(files)[-1] # print(surffix) # .jpg # 02.生成所有大小寫字母的串列 res = list(string.ascii_letters) # print(string.ascii_letters) # 運行結果;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ # 03.將0-9添加到res中 for i in range(0, 10): res.append(str(i)) # 04.隨機生成檔案名: mystr = "".join(random.sample(res, 10)) # sample隨機生成10個字符 # print(mystr) # bJpED6dj2Y # 05.將檔案名和后綴拼接 print(mystr+surffix)
2. sys 系統相關模塊
import sys
print(sys.path)
res = sys.stdin
print(res)
3. math模塊
import math
# print(math.pi) # 3.141592653589793
print(math.factorial(5)) # 120
# 冪運算 第一個引數是底數 第二個引數是冪
print(math.pow(2, 3)) # 8.0
# 向上取整和向下取整
print(math.floor(15.999)) # 15
print(math.ceil(15.001)) # 16
# 四舍五入
print(round(123.51, 1)) # 123.5
# 三角函式
print(math.sin(math.pi / 6)) # sin(pi/6) 0.49999999999999994
print(math.cos(math.pi / 3)) # sin(pi/3) 0.5000000000000001
print(math.tan(math.pi / 4)) # sin(pi/6) 0.9999999999999999
# 開方
a = 9
b = 16
print(math.sqrt(a+b)) # 5.0
# 以e為底的指數函式
print(math.exp(a))
# 8103.083927575384
4. random 隨機模塊
import random
# 01.random() 隨機生成[0,1)之間的數 前閉后開
print(random.random()) # 生成[0,1)之間的小數
# 02.randint() 生成范圍內的隨機整數 全閉
print(random.randint(10, 20)) # 生成[10,20]之間的整數
# 03.randrange() 生成范圍內的隨機整數 前閉后開
print(random.randrange(10, 20)) # 生成[10,20)之間的整數
# 04.choice 引數是串列 隨機從串列中取一個 取一次
print(random.choice([1, 2, 3, 4, 5, 6, 77, 8, 9]))
# 05.sample 的第一個引數 必須是一個可迭代物件
# 第二個引數代表著從可迭代物件從隨機選取幾個,選取的物件不能重復
print("".join(random.sample(["a", "b", "c", "d"], 3)))
5. datetime 和 time 模塊
import datetime as dt # 引入datetime 模塊并將其命別名為dt
import time
import calendar # 引入日歷模塊
# 01.datetime模塊
# 001.獲取當前時間的具體資訊
print(dt.datetime.now())
# 運行結果:
# 2020-12-26 15:36:36.408129
# 年 月 日 時 分 秒 毫秒
# 002.創建日期
print(dt.date(2020,1,1))
# 年月日 2020-01-01
# 003.創建時間
print(dt.time(16,30,30))
# 時 分 秒: 16:30:30
# 004.timedelta() 括號中的默認引數是天
print(dt.datetime.now()+dt.timedelta(3)) # 2020-12-25 15:50:15.811738
print(dt.datetime.now()+dt.timedelta(hours=3)) # 2020-12-22 18:51:41.723093
print(dt.datetime.now()+dt.timedelta(minutes=10)) # 2020-12-22 16:01:41.723093
# 02.time
# 001.當前時間的時間戳
# 時間戳是指從1970—01-01 0:0:0到現在的秒數 utc時間 也叫格林尼治時間
print(time.time())
# 002.按照指定格式輸出時間
# print(time.strftime("%Y-%m-%d %H:%M:%S")) # 2020-12-22 15:57:49
# 時間格式:
# %Y Year with century as a decimal number.
# %m Month as a decimal number [01,12].
# %d Day of the month as a decimal number [01,31].
# %H Hour (24-hour clock) as a decimal number [00,23].
# %M Minute as a decimal number [00,59].
# %S Second as a decimal number [00,61].
# %z Time zone offset from UTC.
# %a Locale's abbreviated weekday name.
# %A Locale's full weekday name.
# %b Locale's abbreviated month name.
# %B Locale's full month name.
# %c Locale's appropriate date and time representation.
# %I Hour (12-hour clock) as a decimal number [01,12].
# %p Locale's equivalent of either AM or PM.
# 003.ctime 和 asctime 時間格式 輸出的時間格式一樣,
# print(time.asctime()) # Tue Dec 22 15:57:49 2020
# print(time.ctime()) # Tue Dec 22 15:58:35 2020
# 004.sleep() 時間休眠
print("我負責浪")
print(time.sleep(3))
print("你負責漫")
# 005.calender 生成日歷
res = calendar.calendar(2021) # 生成2021年的日歷
print(res)
# 006.判斷是否為閏年
print(calendar.isleap(2020)) # True
# 007.從1988年 到 2020年有多少個閏年
print(calendar.leapdays(1988, 2020)) # 8
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241338.html
標籤:python
上一篇:Python字串的15個基本操作
