Python标准库的常用模块
操作系统相关模块:os 与 os.path
Python 的 os
模块提供了与操作系统进行交互的功能。它允许我们操作文件和目录,获取操作系统相关信息等。os.path
是 os
模块中专门用于处理文件路径的子模块。
os 模块基础操作
-
获取当前工作目录
import os current_dir = os.getcwd() print(f"当前工作目录: {current_dir}")
在上述代码中,
os.getcwd()
函数返回当前 Python 脚本所在的工作目录。 -
更改工作目录
import os new_dir = "/path/to/new/directory" try: os.chdir(new_dir) print(f"已成功切换到目录: {new_dir}") except FileNotFoundError: print(f"目录 {new_dir} 不存在")
os.chdir()
函数用于更改当前工作目录。如果指定的目录不存在,会抛出FileNotFoundError
异常。 -
创建目录
import os new_dir = "new_directory" try: os.mkdir(new_dir) print(f"已成功创建目录: {new_dir}") except FileExistsError: print(f"目录 {new_dir} 已存在")
os.mkdir()
函数用于创建一个新的目录。如果目录已经存在,会抛出FileExistsError
异常。
os.path 模块操作
-
拼接路径
import os base_dir = "/home/user" sub_dir = "documents" file_name = "example.txt" full_path = os.path.join(base_dir, sub_dir, file_name) print(f"拼接后的完整路径: {full_path}")
os.path.join()
函数会根据操作系统的路径分隔符,将多个路径部分正确地拼接在一起。在 Unix - like 系统上,路径分隔符是/
,在 Windows 上是\
。 -
拆分路径
import os path = "/home/user/documents/example.txt" dirname, basename = os.path.split(path) print(f"目录部分: {dirname}") print(f"文件名部分: {basename}")
os.path.split()
函数将路径拆分为目录部分和文件名部分。 -
检查路径是否存在
import os path = "/home/user/documents/example.txt" if os.path.exists(path): print(f"路径 {path} 存在") else: print(f"路径 {path} 不存在")
os.path.exists()
函数用于检查指定的路径是否存在。
文件操作模块:io 与 shutil
io
模块为 Python 提供了通用的 I/O 操作接口,而 shutil
模块则提供了更高级的文件和目录操作功能,如复制、移动和删除等。
io 模块
-
文本文件读取
import io with io.open('example.txt', 'r', encoding='utf - 8') as f: content = f.read() print(f"文件内容:\n{content}")
io.open()
函数以指定的模式打开文件。这里以'r'
(读取模式)打开文件,并指定编码为utf - 8
。with
语句确保文件在使用后正确关闭。 -
文本文件写入
import io content = "这是要写入文件的内容。" with io.open('new_example.txt', 'w', encoding='utf - 8') as f: f.write(content) print(f"已成功写入文件 new_example.txt")
以
'w'
(写入模式)打开文件,如果文件不存在则创建,存在则覆盖原有内容。 -
二进制文件读取与写入
import io # 读取二进制文件 with io.open('image.jpg', 'rb') as f: binary_data = f.read() # 写入二进制文件 with io.open('new_image.jpg', 'wb') as f: f.write(binary_data) print(f"已成功复制二进制文件 new_image.jpg")
以
'rb'
模式读取二进制文件,以'wb'
模式写入二进制文件。
shutil 模块
-
文件复制
import shutil source_file = 'example.txt' destination_file = 'example_copy.txt' shutil.copy(source_file, destination_file) print(f"已成功复制文件 {source_file} 到 {destination_file}")
shutil.copy()
函数将源文件复制到目标文件。 -
目录复制
import shutil source_dir = 'old_directory' destination_dir = 'new_directory' shutil.copytree(source_dir, destination_dir) print(f"已成功复制目录 {source_dir} 到 {destination_dir}")
shutil.copytree()
函数递归地复制整个目录树。 -
文件或目录移动
import shutil source = 'example.txt' destination = 'new_location/example.txt' shutil.move(source, destination) print(f"已成功移动文件 {source} 到 {destination}")
shutil.move()
函数将文件或目录移动到新的位置。如果目标路径是一个目录,文件将被移动到该目录下。 -
删除目录
import shutil directory = 'old_directory' shutil.rmtree(directory) print(f"已成功删除目录 {directory}")
shutil.rmtree()
函数递归地删除目录及其所有内容。
日期和时间模块:datetime 与 time
datetime
模块提供了处理日期和时间的类,而 time
模块则提供了更底层的时间相关函数。
datetime 模块
-
获取当前日期和时间
from datetime import datetime now = datetime.now() print(f"当前日期和时间: {now}") print(f"年: {now.year}") print(f"月: {now.month}") print(f"日: {now.day}") print(f"时: {now.hour}") print(f"分: {now.minute}") print(f"秒: {now.second}")
datetime.now()
函数返回当前的日期和时间。通过访问datetime
对象的属性,可以获取年、月、日等具体信息。 -
日期和时间的格式化输出
from datetime import datetime now = datetime.now() formatted_date = now.strftime("%Y-%m-%d %H:%M:%S") print(f"格式化后的日期和时间: {formatted_date}")
strftime()
方法根据指定的格式字符串将datetime
对象格式化为字符串。常见的格式代码有%Y
(四位数年份)、%m
(两位数月份)、%d
(两位数日期)等。 -
日期和时间的运算
from datetime import datetime, timedelta now = datetime.now() one_week_later = now + timedelta(days = 7) print(f"一周后的日期和时间: {one_week_later}")
timedelta
类用于表示两个datetime
对象之间的时间差。可以通过对datetime
对象进行加、减timedelta
对象来进行日期和时间的运算。
time 模块
-
获取当前时间戳
import time timestamp = time.time() print(f"当前时间戳: {timestamp}")
time.time()
函数返回当前时间的时间戳,即从 1970 年 1 月 1 日 00:00:00 UTC 到现在的秒数。 -
时间延迟
import time print("开始延迟") time.sleep(5) print("延迟结束")
time.sleep()
函数使程序暂停执行指定的秒数。这里程序会暂停 5 秒。 -
格式化时间输出
import time current_time = time.localtime() formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time) print(f"格式化后的本地时间: {formatted_time}")
time.localtime()
函数将时间戳转换为本地时间的struct_time
对象,time.strftime()
函数根据格式字符串将struct_time
对象格式化为字符串。
数学运算模块:math 与 random
math
模块提供了常用的数学函数,而 random
模块用于生成随机数。
math 模块
-
基本数学运算
import math num = 4.56 rounded_up = math.ceil(num) rounded_down = math.floor(num) print(f"向上取整: {rounded_up}") print(f"向下取整: {rounded_down}")
math.ceil()
函数将数字向上取整,math.floor()
函数将数字向下取整。 -
三角函数运算
import math angle = math.pi / 4 sine_value = math.sin(angle) cosine_value = math.cos(angle) print(f"正弦值: {sine_value}") print(f"余弦值: {cosine_value}")
math.sin()
和math.cos()
函数分别计算给定角度(以弧度为单位)的正弦值和余弦值。这里的角度是 $\frac{\pi}{4}$ 弧度,即 45 度。 -
对数运算
import math num = 100 log_value = math.log(num) log10_value = math.log10(num) print(f"自然对数: {log_value}") print(f"以 10 为底的对数: {log10_value}")
math.log()
函数计算自然对数(以e
为底),math.log10()
函数计算以 10 为底的对数。
random 模块
-
生成随机整数
import random random_int = random.randint(1, 10) print(f"生成的随机整数: {random_int}")
random.randint(a, b)
函数生成一个在a
(包括)和b
(包括)之间的随机整数。 -
生成随机浮点数
import random random_float = random.random() print(f"生成的随机浮点数: {random_float}")
random.random()
函数生成一个在 0(包括)和 1(不包括)之间的随机浮点数。 -
从序列中随机选择元素
import random fruits = ['apple', 'banana', 'cherry'] random_fruit = random.choice(fruits) print(f"随机选择的水果: {random_fruit}")
random.choice(seq)
函数从非空序列seq
中随机选择一个元素。
数据处理模块:collections 与 json
collections
模块提供了一些有用的数据结构,而 json
模块用于处理 JSON 数据。
collections 模块
-
Counter 类
from collections import Counter words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'banana'] word_count = Counter(words) print(f"单词计数: {word_count}") most_common_word = word_count.most_common(1)[0][0] print(f"最常见的单词: {most_common_word}")
Counter
类用于统计可迭代对象中元素的出现次数。most_common(n)
方法返回出现次数最多的n
个元素及其计数。 -
defaultdict 类
from collections import defaultdict student_scores = defaultdict(list) student_scores['Alice'].append(85) student_scores['Alice'].append(90) student_scores['Bob'].append(78) print(f"学生成绩: {student_scores}")
defaultdict
类提供了一个字典,当访问不存在的键时,会自动创建一个默认值。这里默认值是一个空列表。 -
deque 类
from collections import deque queue = deque([1, 2, 3]) queue.append(4) removed_item = queue.popleft() print(f"移除的元素: {removed_item}") print(f"队列剩余元素: {queue}")
deque
(双端队列)类提供了一个可以在两端进行添加和删除操作的序列。append()
方法在右端添加元素,popleft()
方法从左端移除元素。
json 模块
-
将 Python 对象转换为 JSON 字符串
import json data = { "name": "Alice", "age": 30, "city": "New York" } json_data = json.dumps(data) print(f"JSON 字符串: {json_data}")
json.dumps()
函数将 Python 对象(如字典、列表等)转换为 JSON 格式的字符串。 -
将 JSON 字符串转换为 Python 对象
import json json_str = '{"name": "Bob", "age": 25, "city": "Los Angeles"}' python_obj = json.loads(json_str) print(f"Python 对象: {python_obj}") print(f"姓名: {python_obj['name']}")
json.loads()
函数将 JSON 格式的字符串转换为 Python 对象。 -
将 Python 对象写入 JSON 文件
import json data = [ {"name": "Charlie", "age": 28, "city": "Chicago"}, {"name": "David", "age": 32, "city": "Houston"} ] with open('data.json', 'w') as f: json.dump(data, f) print(f"已成功将数据写入 data.json")
json.dump()
函数将 Python 对象直接写入文件对象,以 JSON 格式保存。 -
从 JSON 文件读取数据到 Python 对象
import json with open('data.json', 'r') as f: data = json.load(f) print(f"从文件读取的 Python 对象: {data}")
json.load()
函数从文件对象中读取 JSON 数据,并将其转换为 Python 对象。
网络编程模块:socket 与 urllib
socket
模块提供了底层的网络通信功能,而 urllib
模块则用于处理 URL 相关的操作,包括获取网页内容等。
socket 模块
-
简单的 TCP 服务器
import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('127.0.0.1', 12345)) server_socket.listen(1) print("服务器正在监听 127.0.0.1:12345") while True: client_socket, client_address = server_socket.accept() print(f"接受来自 {client_address} 的连接") data = client_socket.recv(1024) client_socket.sendall(b"已收到你的消息: " + data) client_socket.close()
首先创建一个 TCP 套接字(
socket.SOCK_STREAM
),绑定到本地地址127.0.0.1
和端口12345
,然后开始监听连接。当有客户端连接时,接收客户端发送的数据,并回显一条包含客户端数据的消息。 -
简单的 TCP 客户端
import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(('127.0.0.1', 12345)) message = b"Hello, server!" client_socket.sendall(message) response = client_socket.recv(1024) print(f"服务器响应: {response}") client_socket.close()
创建一个 TCP 客户端套接字,连接到服务器的地址和端口,发送一条消息,然后接收服务器的响应并打印。
urllib 模块
-
获取网页内容
from urllib.request import urlopen url = 'https://www.example.com' with urlopen(url) as response: html_content = response.read().decode('utf - 8') print(f"网页内容:\n{html_content}")
urlopen()
函数打开指定的 URL,并返回一个类似文件的对象。可以使用read()
方法读取网页内容,并使用decode()
方法将字节数据解码为字符串。 -
发送带有参数的请求
from urllib.parse import urlencode from urllib.request import urlopen base_url = 'https://www.example.com/search' params = { 'q': 'python programming', 'page': 1 } query_string = urlencode(params) full_url = f"{base_url}?{query_string}" with urlopen(full_url) as response: content = response.read().decode('utf - 8') print(f"搜索结果内容:\n{content}")
urlencode()
函数将字典形式的参数编码为 URL 查询字符串。然后将查询字符串附加到基本 URL 上,发送请求并获取响应内容。
多线程与多进程模块:threading 与 multiprocessing
threading
模块用于实现多线程编程,multiprocessing
模块用于实现多进程编程。
threading 模块
-
简单的线程示例
import threading def print_numbers(): for i in range(1, 6): print(f"线程 1: {i}") def print_letters(): for letter in 'abcde': print(f"线程 2: {letter}") thread1 = threading.Thread(target = print_numbers) thread2 = threading.Thread(target = print_letters) thread1.start() thread2.start() thread1.join() thread2.join()
创建两个线程,一个线程打印数字,另一个线程打印字母。
start()
方法启动线程,join()
方法等待线程执行完毕。 -
线程同步:锁的使用
import threading shared_variable = 0 lock = threading.Lock() def increment(): global shared_variable for _ in range(100000): lock.acquire() shared_variable += 1 lock.release() threads = [] for _ in range(5): thread = threading.Thread(target = increment) threads.append(thread) thread.start() for thread in threads: thread.join() print(f"共享变量的值: {shared_variable}")
这里有一个共享变量
shared_variable
,多个线程对其进行递增操作。为了避免竞态条件,使用Lock
对象来同步线程。acquire()
方法获取锁,release()
方法释放锁。
multiprocessing 模块
-
简单的进程示例
import multiprocessing def square_numbers(numbers): for num in numbers: print(f"进程: {num * num}") if __name__ == '__main__': numbers = [1, 2, 3, 4, 5] process = multiprocessing.Process(target = square_numbers, args = (numbers,)) process.start() process.join()
创建一个进程来计算数字列表中每个数字的平方。在使用
multiprocessing
模块时,通常需要在if __name__ == '__main__':
块中执行相关代码,以确保在 Windows 等操作系统上正确运行。 -
进程间通信:队列的使用
import multiprocessing def producer(queue): for i in range(5): queue.put(i) print(f"生产者放入: {i}") def consumer(queue): while True: item = queue.get() if item is None: break print(f"消费者取出: {item}") if __name__ == '__main__': queue = multiprocessing.Queue() producer_process = multiprocessing.Process(target = producer, args = (queue,)) consumer_process = multiprocessing.Process(target = consumer, args = (queue,)) producer_process.start() consumer_process.start() producer_process.join() queue.put(None) consumer_process.join()
生产者进程将数字放入队列,消费者进程从队列中取出数字。通过在队列中放入
None
来通知消费者进程结束。
调试与测试模块:pdb 与 unittest
pdb
模块用于调试 Python 代码,unittest
模块用于编写和运行单元测试。
pdb 模块
- 简单调试示例
在import pdb def add_numbers(a, b): result = a + b pdb.set_trace() return result num1 = 5 num2 = 10 sum_result = add_numbers(num1, num2) print(f"两数之和: {sum_result}")
add_numbers
函数中,使用pdb.set_trace()
方法设置断点。当程序执行到该断点时,会进入调试模式。在调试模式下,可以查看变量的值,单步执行代码等。常用的调试命令有n
(执行下一行)、s
(进入函数调用)、c
(继续执行直到下一个断点)等。
unittest 模块
-
编写单元测试
import unittest def add(a, b): return a + b class TestAddition(unittest.TestCase): def test_add(self): result = add(3, 5) self.assertEqual(result, 8) if __name__ == '__main__': unittest.main()
首先定义一个
add
函数,然后创建一个测试用例类TestAddition
,它继承自unittest.TestCase
。在测试用例类中,定义一个测试方法test_add
,使用self.assertEqual()
方法来断言函数的返回值是否符合预期。最后,使用unittest.main()
来运行测试。 -
测试多个用例
import unittest def multiply(a, b): return a * b class TestMultiplication(unittest.TestCase): def test_multiply(self): result1 = multiply(2, 3) self.assertEqual(result1, 6) result2 = multiply(0, 5) self.assertEqual(result2, 0) if __name__ == '__main__': unittest.main()
在这个示例中,测试方法
test_multiply
中包含了两个测试用例,分别测试multiply
函数在不同输入下的正确性。
以上就是 Python 标准库中一些常用模块的详细介绍,通过合理使用这些模块,可以大大提高 Python 程序的开发效率和功能丰富度。在实际开发中,根据具体需求灵活选择和组合这些模块,能够实现各种复杂的任务。