20260714python暑假班第三次作业

发布时间:2026/7/29 4:45:40
20260714python暑假班第三次作业 第1题(IO流-文本)编写程序:读取article.txt,统计文件中英文单词出现总数量(只统计单词忽略数字、标点符号)并将统计结果写入result.txt要求:使用with语法,文件编码utf-8import re def count_english_words(text): 统计文本中的英文单词数量 只统计由字母组成的单词忽略数字和标点符号 # 使用正则表达式匹配英文单词只包含字母 # 匹配连续的英文字母包括大写和小写 words re.findall(r[a-zA-Z], text) return len(words) def main(): try: # 使用with语法读取article.txt with open(article.txt, r, encodingutf-8) as file: content file.read() # 统计英文单词数量 word_count count_english_words(content) # 使用with语法写入result.txt with open(result.txt, w, encodingutf-8) as file: file.write(f英文单词总数量: {word_count}) print(f统计完成英文单词总数量: {word_count}) print(结果已写入 result.txt) except FileNotFoundError: print(错误: 找不到 article.txt 文件) except Exception as e: print(f发生错误: {e}) if __name__ __main__: main()第2题(IO流-读写模式综合)现有文件record.log,编写代码:1.如果文件不存在则创建2.在文件末尾追加一行:2026-07-27 操作完成3.追加完成后读取文件全部内容打印到控制台提示:注意光标位置,选对打开模式import os from datetime import datetime def main(): filename record.log try: # 1. 如果文件不存在则创建并在末尾追加内容 # 使用 a 模式如果文件不存在则创建存在则在末尾追加同时支持读取 with open(filename, a, encodingutf-8) as file: # 追加一行日志信息 log_entry 2026-07-27 操作完成 file.write(log_entry \n) # 2. 追加完成后读取文件全部内容 # 注意追加操作后文件指针在末尾需要移动到文件开头才能读取 file.seek(0) # 将文件指针移动到文件开头 # 读取全部内容 content file.read() # 打印到控制台 print(文件内容) print(- * 40) print(content) print(- * 40) except Exception as e: print(f发生错误: {e}) if __name__ __main__: main()第3题(模块-自定义模块)1.创建自定义模块math_tool.py,内部实现两个函数:- calc_area(r):计算圆面积- calc_circum(r):计算圆周长2.在另一个文件main.py,用两种不同导入方式调用这两个函数:- 方式1:import math_tool- 方式2:from math_tool import calc_area, calc_circum写出两个文件完整代码# math_tool.py - 自定义数学工具模块 import math def calc_area(r): 计算圆的面积 参数: r - 圆的半径 返回: 圆的面积 if r 0: raise ValueError(半径不能为负数) return math.pi * r ** 2 def calc_circum(r): 计算圆的周长 参数: r - 圆的半径 返回: 圆的周长 if r 0: raise ValueError(半径不能为负数) return 2 * math.pi * r # 模块测试代码只在直接运行模块时执行 if __name__ __main__: # 测试函数 test_radius 5 print(f半径 {test_radius} 的圆面积: {calc_area(test_radius):.2f}) print(f半径 {test_radius} 的圆周长: {calc_circum(test_radius):.2f})# main.py - 演示两种不同的模块导入方式 import math_tool # 方式1导入整个模块 from math_tool import calc_area, calc_circum # 方式2导入特定函数 def main(): radius 10 print( * 50) print(方式1: import math_tool) print(- * 50) # 方式1使用模块名调用函数 area1 math_tool.calc_area(radius) circum1 math_tool.calc_circum(radius) print(f半径 {radius} 的圆面积: {area1:.2f}) print(f半径 {radius} 的圆周长: {circum1:.2f}) print(\n * 50) print(方式2: from math_tool import calc_area, calc_circum) print(- * 50) # 方式2直接调用函数无需模块名前缀 area2 calc_area(radius) circum2 calc_circum(radius) print(f半径 {radius} 的圆面积: {area2:.2f}) print(f半径 {radius} 的圆周长: {circum2:.2f}) print(\n * 50) print(两种方式比较:) print(- * 50) print(方式1 优点: 命名空间清晰避免函数名冲突) print(方式1 缺点: 每次调用都需要写模块名前缀) print(方式2 优点: 代码简洁直接调用函数) print(方式2 缺点: 可能导致命名冲突) if __name__ __main__: main()第4题(模块-标准库os模块)使用os标准库完成:1.遍历当前文件夹打印所有后缀为.txt的文件名:2.创建文件夹backup3.加判断条件如果backup文件夹已存在不要报错import os def main(): # 1. 遍历当前文件夹打印所有后缀为.txt的文件名 print(所有 .txt 文件:) for item in os.listdir(.): if item.endswith(.txt) and os.path.isfile(item): print(f {item}) # 2. 创建文件夹backup # 3. 如果backup文件夹已存在不要报错 folder_name backup if not os.path.exists(folder_name): os.mkdir(folder_name) print(f文件夹 {folder_name} 创建成功) else: print(f文件夹 {folder_name} 已存在) if __name__ __main__: main()第5题使用os和os.path以及函数的递归完成:给出一个路径遍历当前路径所有的文件及文件夹打印输出所有的文件(遇到文件输出路径遇到文件夹继续进文件夹)import os def list_all(path): 递归列出所有文件和文件夹的路径 try: for item in os.listdir(path): full_path os.path.join(path, item) print(full_path) if os.path.isdir(full_path): list_all(full_path) # 递归进入文件夹 except PermissionError: pass # 忽略权限错误 def main(): path input(请输入目录路径直接回车为当前目录: ).strip() if not path: path . if os.path.exists(path) and os.path.isdir(path): list_all(path) else: print(路径无效) if __name__ __main__: main()