Python技能手册 - 模块module

系列

Python常用技能手册 - 基础语法

Python常用技能手册 - 模块module

Python常用技能手册 - 包package

目录

[module 模块指什么](#module 模块指什么)

[typing 类型](#typing 类型)

[int 整数](#int 整数)

[float 浮点数](#float 浮点数)

[str 字符串](#str 字符串)

[bool 布尔值](#bool 布尔值)

[TypeVar 类型变量](#TypeVar 类型变量)

functools

[functools.partial() 函数偏置](#functools.partial() 函数偏置)

[functools.lru_cache() 函数缓存](#functools.lru_cache() 函数缓存)

sorted

列表排序

元组排序

字符串排序(按长度)

字符串排序(按字典序)

类排序

Lambda表达式排序

copy

[copy.copy() 浅拷贝](#copy.copy() 浅拷贝)

[copy.deepcopy() 深拷贝](#copy.deepcopy() 深拷贝)

heapq

[heapq.heappush() 创建小顶堆](#heapq.heappush() 创建小顶堆)

[heapq.nsmallest() 获取前n个最小值](#heapq.nsmallest() 获取前n个最小值)

[heapq.heapfy() 列表转小顶堆](#heapq.heapfy() 列表转小顶堆)

os

[os.getcwd() 获取当前工作目录](#os.getcwd() 获取当前工作目录)

[os.getenv()和os.putenv() 环境变量](#os.getenv()和os.putenv() 环境变量)

[os.listdir() 返回目录信息](#os.listdir() 返回目录信息)

[os.stat(file) 获取文件属性](#os.stat(file) 获取文件属性)

[os.chmod(file) 修改文件权限](#os.chmod(file) 修改文件权限)

[os.mkdir(name) 创建目录](#os.mkdir(name) 创建目录)

[os.makedirs() 递归创建目录](#os.makedirs() 递归创建目录)

[os.remove(file) 删除文件](#os.remove(file) 删除文件)

[os.rmdir(name) 删除目录](#os.rmdir(name) 删除目录)

[os.removedirs() 删除多个目录](#os.removedirs() 删除多个目录)

[os.system() 运行shell命令](#os.system() 运行shell命令)

shutil

[copy()和copy2() 复制](#copy()和copy2() 复制)

move()

rmtree()

disk_usage()

chown()

time

[time.time() 获取时间戳](#time.time() 获取时间戳)

[time.sleep() 休眠](#time.sleep() 休眠)

[time.localtime([timestamp]) 获取当地时间](#time.localtime([timestamp]) 获取当地时间)

[time.strftime(format[, t]) 格式化](#time.strftime(format[, t]) 格式化)

datetime

[三种基本类型 data、time、datetime](#三种基本类型 data、time、datetime)

date

[date() 初始化日期](#date() 初始化日期)

time

[time() 初始化时间](#time() 初始化时间)

datetime

[datetime.strftime() 时间转字符串](#datetime.strftime() 时间转字符串)

[datetime.strptime() 字符串转日期](#datetime.strptime() 字符串转日期)

math和cmath

random

pickle

shelve


module 模块指什么

Python模块就是包含Python代码的文件,模块名就是文件名(去掉.py后缀)。模块可以包含函数、类、变量等。模块可以提高代码的可维护性和重复使用,避免函数名和变量名冲突。

typing 类型

Python的typing库提供了类型提示的功能,可以在代码中指定参数和返回值的类型。

int 整数

python 复制代码
def add_numbers(a: int, b: int) -> int:
    return a + b

float 浮点数

python 复制代码
def divide_numbers(a: float, b: float) -> float:
    return a / b

str 字符串

python 复制代码
def greet(name: str) -> str:
    return "Hello, " + name + "!"

bool 布尔值

python 复制代码
def is_positive(number: int) -> bool:
    return number > 0

TypeVar 类型变量

python 复制代码
from typing import TypeVar

T = TypeVar('T')

def get_first_element(arr: list[T]) -> T:
    return arr[0]

functools

functools 是 Python 的一个内置模块,它提供了对高阶函数(接受其他函数作为参数的函数)的支持,以及一些有用的工具函数。

functools.partial() 函数偏置

partial() 是一个非常有用的函数,它用于将一个函数的某些参数固定下来,返回一个新的函数。这样,你就可以创建一个新的函数,该函数在调用时将始终使用指定的参数值。

下面的例子将message参数固定下来,生成了一个函数

python 复制代码
from functools import partial

def greet(name, message):
    print(f"{message}, {name}!")

greet_with_hello = partial(greet, message="Hello")
greet_with_hello("Alice")  # 输出: Hello, Alice!

再举个例子,

python 复制代码
import functools

int2 = functools.partial(int, base=2)
int2('1000000')     # 64

functools.lru_cache() 函数缓存

lru_cache() 是一个装饰器,它可以用来为 Python 函数添加缓存功能。它使用 LRU(最近最少使用)策略来缓存函数调用的结果。当同一个函数被多次调用时,它可以提高性能,因为它可以避免重复计算。

python 复制代码
from functools import lru_cache

@lru_cache(maxsize=128)
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))  # 输出: 55 (因为结果被缓存了)

sorted

与sort方法不同,sorted()函数会生成一个新的列表,而不是在原始的列表上进行操作。

列表排序

python 复制代码
list.sort()    # 列表排序
list.reverse()    # 列表逆序

元组排序

python 复制代码
sorted(tuple)    # 元组排序
reversed(tuple)    # 元组逆序

字符串排序(按长度)

python 复制代码
words = ["apple", "banana", "cherry"]
sorted_words = sorted(words, key=len)
print(sorted_words)  # ['apple', 'cherry', 'banana']

字符串排序(按字典序)

python 复制代码
strings = ["apple", "banana", "cherry", "date"]
sorted_strings = sorted(strings)
print(sorted_strings)    # ['apple', 'banana', 'cherry', 'date']

类排序

python 复制代码
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def __lt__(self, other):
        # 实现 less than方法,来实现排序
        return self.age < other.age

people = [Person("Alice", 25), Person("Bob", 30), Person("Charlie", 20)]
sorted_people = sorted(people)
print([p.name for p in sorted_people])  # ['Charlie', 'Alice', 'Bob']

Lambda表达式排序

python 复制代码
# 对元组列表按照第一个元素升序排序   
my_list = [(2, 'b'), (3, 'c'), (1, 'a')]   
sorted_list = sorted(my_list, key=lambda x: x[0])   
print(sorted_list) # 输出 [(1, 'a'), (2, 'b'), (3, 'c')]

copy

copy.copy() 浅拷贝

浅拷贝是指创建一个新对象,但是这个新对象中的元素是原对象的引用。新对象中的元素和原对象中的元素指向同一个内存地址。

python 复制代码
import copy

list1 = [1, 2, [3, 4]]
list2 = copy.copy(list1)

print(list1)  # [1, 2, [3, 4]]
print(list2)  # [1, 2, [3, 4]]

list1[2][0] = 5

print(list1)  # [1, 2, [5, 4]]
print(list2)  # [1, 2, [5, 4]]    # 修改一个元素,另一个元素也会发生变化

copy.deepcopy() 深拷贝

在 Python 中,可以使用 copy 模块中的 deepcopy() 函数来实现二维数组的任意拷贝。deepcopy() 函数可以递归地复制对象中的所有元素,包括嵌套的对象。

python 复制代码
import copy

# 原始二维数组
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# 使用 deepcopy() 函数进行拷贝
arr_copy = copy.deepcopy(arr)

# 修改拷贝后的数组
arr_copy[0][0] = 0

# 打印原始数组和拷贝后的数组
print("原始数组:", arr)
print("拷贝后的数组:", arr_copy)

heapq

heapq是一个Python内置模块,提供了对堆队列(heap queue)的基本支持。它可以对可迭代对象进行原地堆排序并返回一个排序后的列表。

heapq.heappush() 创建小顶堆

python 复制代码
import heapq
a = []   #创建一个空堆
heapq.heappush(a,18)
heapq.heappush(a,1)
heapq.heappush(a,20)
heapq.heappush(a,10)
heapq.heappush(a,5)
heapq.heappush(a,200)
print(a)    # [1, 5, 10, 18, 20, 200]

**heapq.nsmallest()**获取前n个最小值

python 复制代码
import heapq

a = [1, 5, 0, 100]
n_small_list = heapq.nsmallest(3, a)    # [1, 5, 10]

**heapq.heapfy()**列表转小顶堆

python 复制代码
import heapq

a = [1, 5, 200, 18, 10, 200]
heapq.heapify(a)
print(a)    # [1, 5, 200, 18, 10, 200]

os

os模块是Python标准库中的一个用于访问操作系统功能的模块,提供了一种可移植的方法使用操作系统的功能。

os.getcwd() 获取当前工作目录

得到当前工作目录,即当前python脚本工作的目录路径。

python 复制代码
import os

print(os.getcwd())  # 输出目录
print(os.getcwdb())  # 输出目录的字节类型
print(type(os.getcwdb()))  # 输出目录的字节类型 <class 'bytes'>

os.getenv()和os.putenv() 环境变量

分别用来读取和设置环境变量。

python 复制代码
import os

print(os.getenv("path"))    # C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS\System32\Wbem;...

os.listdir() 返回目录信息

返回指定目录下第一层的所有文件和目录名。

python 复制代码
import os

print(os.listdir()) # ['.idea', 'main.py', 'main1.py', 'main2.py', 'main3.py', 'main32.py', 'resources']

os.stat(file) 获取文件属性

获得文件属性。

python 复制代码
import os

print(os.stat(r"D:\Code\CodePython\test-0811\main32.py"))
# os.stat_result(st_mode=33206, st_ino=30680772461484765 ...

具体含义

`st_mode`:文件类型和访问权限的标志位,可以通过 `stat.S_ISDIR()` 和 `stat.S_ISREG()` 等函数判断文件类型。

`st_ino`:文件的 inode 号,唯一标识一个文件。

`st_dev`:文件所在的设备号。

`st_nlink`:文件的硬链接数。

`st_uid`:文件所有者的用户 ID。

`st_gid`:文件所有者的组 ID。

`st_size`:文件大小,单位是字节。

`st_atime`:文件的最近访问时间,是一个时间戳。

`st_mtime`:文件的最近修改时间,是一个时间戳。

`st_ctime`:文件的创建时间,是一个时间戳。

os.chmod(file) 修改文件权限

python 复制代码
import os, sys, stat

# 假定 /tmp/foo.txt 文件存在,设置文件可以通过用户组执行
os.chmod("/tmp/foo.txt", stat.S_IXGRP)

os.mkdir(name) 创建目录

创建一个名为 path 的目录,应用以数字表示的权限模式 mode。

如果目录已存在,则抛出 FileExistsError 异常。

某些系统会忽略 mode。如果没有忽略它,那么将首先从它中减去当前的 umask 值。如果除最后 9 位(即 mode 八进制的最后 3 位)之外,还设置了其他位,则其他位的含义取决于各个平台。在某些平台上,它们会被忽略,应显式调用 chmod() 进行设置。

本函数支持 基于目录描述符的相对路径。

python 复制代码
import os

for x in range(10):
    path = r"D:\Code\CodePython\test-0811\dir" + str(x)
    if not os.path.exists(path):
        os.mkdir(path)

os.makedirs() 递归创建目录

python 复制代码
import os

for x in range(10):
    path = r"D:\Code\CodePython\test-0811\dir\dirr" + str(x)
    if not os.path.exists(path):
        os.makedirs(path)

os.remove(file) 删除文件

删除一个文件.

python 复制代码
import os

os.remove(r"D:\xxx.py")

os.rmdir(name) 删除目录

目录下面必须是空的,才行。推荐使用shutil.rmtree()

python 复制代码
import os

for x in range(10):
    path = r"D:\Code\CodePython\test-0811\dir"
    os.rmdir(path)

os.removedirs() 删除多个目录

也不推荐,推荐使用shutil.rmtree()。

os.system() 运行shell命令

涉及安全问题,一般不用。

shutil

shutil 是 Python 的一个标准库模块,它的全称是"shell utility",意为"shell工具"。它提供了文件和文件集合的高层操作。这个模块中的函数提供了一些文件操作的功能,这些功能通常比使用 os 模块中的函数更方便。

copy()和copy2() 复制

这两个函数用于复制文件。copy() 只会复制源文件的内容,而不会复制元数据(如修改日期和时间)。copy2() 会尝试复制源文件的元数据。

python 复制代码
import shutil

# 源文件路径
src_file = "/path/to/source/file.txt"
# 目标文件路径
dst_file = "/path/to/destination/file.txt"

# 复制文件
shutil.copy(src_file, dst_file)

# 复制文件并尽可能保留元数据
shutil.copy2(src_file, dst_file)

move()

这个函数用于将文件或目录从一个位置移动到另一个位置。

python 复制代码
import shutil

# 源文件路径
src_file = "/path/to/source/file.txt"
# 目标文件路径(可以同时指定新名称)
dst_file = "/path/to/destination/new_file.txt"

# 移动文件
shutil.move(src_file, dst_file)

rmtree()

这个函数用于删除目录及其所有内容。

python 复制代码
import shutil
import os

# 要删除的目录路径
dir_path = "/path/to/directory"

# 检查目录是否存在,如果存在则删除整个目录及其内容
if os.path.exists(dir_path):
    shutil.rmtree(dir_path)

disk_usage()

这个函数返回指定路径的磁盘使用情况。

python 复制代码
print(shutil.disk_usage(path))
# usage(total=776039559168, used=173954117632, free=602085441536)

chown()

这个函数用于改变文件或目录的所有者和组。

python 复制代码
import os

# 打开一个文件并设置权限
with open('example.txt', 'w') as f:
    os.chmod(f.fileno(), 0o777)  
    # 这里的0o777只是一个示例,实际使用时需要根据你的需求设置合适的权限

time

time模块提供了一系列函数,用于获取当前时间、时间格式化、时间戳等操作。

time.time() 获取时间戳

返回当前时间的时间戳,以秒为单位,浮点数表示。

python 复制代码
print(time.time())  # 1703581829.3947577

time.sleep() 休眠

使程序暂停指定的秒数。

python 复制代码
time.sleep(1)    # 休眠1s

time.localtime([timestamp]) 获取当地时间

将给定的时间戳转换为本地时间,返回一个包含年、月、日、时、分、秒等信息的time.struct_time对象。

python 复制代码
import time
    
# 获取指定时间的当地时间
print(time.localtime(time.time()))
# time.struct_time(tm_year=2023, tm_mon=12, tm_mday=26, tm_hour=17, tm_min=14, tm_sec=4, tm_wday=1, tm_yday=360, tm_isdst=0)

# 查看返回结果的类型
print(type(time.localtime(time.time())))
# <class 'time.struct_time'>

# 获取当前时间的当地时间
print(time.localtime())
# time.struct_time(tm_year=2023, tm_mon=12, tm_mday=26, tm_hour=17, tm_min=14, tm_sec=4, tm_wday=1, tm_yday=360, tm_isdst=0)

time.strftime(format[, t]) 格式化

将时间对象格式化为字符串。

python 复制代码
import time

t = (2009, 2, 17, 17, 3, 38, 1, 48, 0)
t = time.mktime(t)
print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))	# Feb 17 2009 09:03:38

datetime

三种基本类型 data、time、datetime

datetime模块提供了日期和时间数据类型,包括date、time和datetime。

  • datetime.date:表示日期,包括年、月、日。
  • datetime.time:表示时间,包括时、分、秒、纳秒。
  • datetime.datetime:表示日期和时间,包括年、月、日、时、分、秒、纳秒。

date

date表示日期(年、月、日)。

date() 初始化日期

python 复制代码
from datetime import date

# 创建一个日期对象
d = date(2023, 3, 15)
print(d)  # 输出:2023-03-15

# 获取日期对象的信息
print(d.year)  # 输出:2023
print(d.month)  # 输出:3
print(d.day)  # 输出:15

time

time表示时间(时、分、秒、微秒)。

time() 初始化时间

python 复制代码
from datetime import time

# 创建一个时间对象
t = time(13, 45, 30)
print(t)  # 输出:13:45:30

# 获取时间对象的信息
print(t.hour)  # 输出:13
print(t.minute)  # 输出:45
print(t.second)  # 输出:30

datetime

datetime表示日期和时间。

datetime.strftime() 时间转字符串

将日期时间格式化为字符串。

python 复制代码
from datetime import datetime

now = datetime.now()

month = now.strftime("%m")
print("month:", month)  # month: 12

day = now.strftime("%d")
print("day:", day)  # day: 26

time = now.strftime("%H:%M:%S")
print("time:", time)    # time: 17:20:51

date_time = now.strftime("%Y-%m-%d, %H:%M:%S")
print("date and time:", date_time)  # date and time: 2023-12-26, 17:20:51

datetime.strptime() 字符串转日期

将字符串解析为日期时间。

python 复制代码
from datetime import datetime

date_string = "21 June, 2018"

print("date_string =", date_string)
print("type of date_string =", type(date_string))

date_object = datetime.strptime(date_string, "%d %B, %Y")

print("date_object =", date_object)
print("type of date_object =", type(date_object))
  • %d Day of the month as a zero-padded decimal. 01, 02, ..., 31
  • %-d Day of the month as a decimal number. 1, 2, ..., 30
  • %b Abbreviated month name. Jan, Feb, ..., Dec
  • %B Full month name. January, February, ...
  • %m Month as a zero-padded decimal number. 01, 02, ..., 12
  • %-m Month as a decimal number. 1, 2, ..., 12
  • %y Year without century as a zero-padded decimal number. 00, 01, ..., 99
  • %-y Year without century as a decimal number. 0, 1, ..., 99
  • %Y Year with century as a decimal number. 2013, 2019 etc.
  • %H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
  • %-H Hour (24-hour clock) as a decimal number. 0, 1, ..., 23

math和cmath

random

随机模块,用于生成随机数。

pickle

shelve

相关推荐
吾爱星辰1 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer1 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
FreakStudio1 小时前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
IT良1 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
yufei-coder1 小时前
掌握 C# 中的 LINQ(语言集成查询)
windows·vscode·c#·visual studio
丶21361 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家2 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技2 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm