python 3 第一题IO流-文本编写程序读取 article.txt 统计文件中英文单词出现总数量只统计单词忽略数字、标点符号并将统计结果写入 result.txt 。要求使用 with 语法文件编码 utf-8 。【代码】with open(article.txt,r,encodingutf-8) as file: content file.read() count 0 judgment False for word in content: if (a word z) or (A word Z): if not judgment: count 1 judgment True else: judgment False with open(result.txt,w,encodingutf-8) as file: file.write(str(count))【运行结果】第二题IO流-读写模式综合现有文件 record.log 编写代码 1. 如果文件不存在则创建 2. 在文件末尾追加一行 2026-07-27 操作完成 ; 3. 追加完成后读取文件全部内容打印到控制台。 提示注意光标位置选对打开模式。【代码】with open(D:\\VS Code 代码\\pythoncode\\作业\\第三次作业\\record.log,a,encodingutf-8) as file: file.write(\n2026-07-27 操作完成) file.seek(0) print(file.read())【运行结果】第三题模块‑自定义模块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写出两个文件完整代码。【代码】import math def calc_area(r): return math.pi * r * r def calc_circum(r): return 2 * math.pi * rr 1 print(方式1) import math_tool print(f半径为{r}的圆面积{math_tool.calc_area(r):.2f}) print(f半径为{r}的圆周长{math_tool.calc_circum(r):.2f}) print(方式2) from math_tool import calc_area,calc_circum print(f半径为{r}的圆面积{math_tool.calc_area(r):.2f}) print(f半径为{r}的圆周长{math_tool.calc_circum(r):.2f})【运行结果】第四题模块-标准库os模块使用 os 标准库完成1. 遍历当前文件夹打印所有后缀为 .txt 的文件名2. 创建文件夹 backup ;3. 加判断条件如果backup文件夹已存在不要报错。【代码】import os for file in os.listdir(.): if file.endswith(.txt): print(file) try: os.mkdir(backup) except: pass【运行结果】第五题递归-os目录遍历使用os和os.path以及函数的递归完成:给出一个路径遍历当前路径所有的文件及文件夹打印输出所有的文件遇到文件输出路径遇到文件夹继续进文件夹【代码】import os def traverse_dir(file_path): for i in os.listdir(file_path): path os.path.join(file_path, i) if os.path.isdir(path): traverse_dir(path) else: print(path) if __name__ __main__: target_path ./ traverse_dir(target_path)【运行结果】

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/28 1:37:33
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/27 19:13:42
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/27 15:27:56
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/27 19:54:03
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 17:20:49
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/28 2:08:29

日新闻

周新闻