{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业任务\n",
    "\n",
    "本作业共六个任务，围绕 `scores.txt` 文件和文件夹的创建、读取、信息获取、删除等操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 01. 创建 scores.txt 并写入成绩信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scores.txt 文件创建成功，内容如下：\n",
      "姓名\t语文\t数学\t英语\n",
      "张三\t85\t76\t91\n",
      "李四\t89\t92\t96\n",
      "王五\t72\t84\t88\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 任务 1：创建 scores.txt 文本文件，并写入成绩信息\n",
    "content = \"姓名\\t语文\\t数学\\t英语\\n\" \\\n",
    "          \"张三\\t85\\t76\\t91\\n\" \\\n",
    "          \"李四\\t89\\t92\\t96\\n\" \\\n",
    "          \"王五\\t72\\t84\\t88\\n\"\n",
    "\n",
    "with open('scores.txt', 'w', encoding='utf-8') as f:\n",
    "    f.write(content)\n",
    "\n",
    "print('scores.txt 文件创建成功，内容如下：')\n",
    "with open('scores.txt', 'r', encoding='utf-8') as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 02. 读取 scores.txt 并计算平均成绩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每个人的平均成绩：\n",
      "张三 的平均成绩为：84.00\n",
      "李四 的平均成绩为：92.33\n",
      "王五 的平均成绩为：81.33\n",
      "\n",
      "每个科目的平均成绩：\n",
      "语文 的平均成绩为：82.00\n",
      "数学 的平均成绩为：84.00\n",
      "英语 的平均成绩为：91.67\n"
     ]
    }
   ],
   "source": [
    "# 任务 2：读取 scores.txt，分别计算每个人的平均成绩和每个科目的平均成绩\n",
    "with open('scores.txt', 'r', encoding='utf-8') as f:\n",
    "    lines = f.readlines()\n",
    "\n",
    "# 去掉换行符并按制表符拆分\n",
    "header = lines[0].strip().split('\\t')       # ['姓名', '语文', '数学', '英语']\n",
    "data = [line.strip().split('\\t') for line in lines[1:] if line.strip()]\n",
    "\n",
    "# 每个人的平均成绩\n",
    "print('每个人的平均成绩：')\n",
    "for row in data:\n",
    "    name = row[0]\n",
    "    scores = [int(x) for x in row[1:]]\n",
    "    avg = sum(scores) / len(scores)\n",
    "    print(f'{name} 的平均成绩为：{avg:.2f}')\n",
    "\n",
    "# 每个科目的平均成绩\n",
    "print('\\n每个科目的平均成绩：')\n",
    "subjects = header[1:]\n",
    "for i, subject in enumerate(subjects):\n",
    "    col = [int(row[i + 1]) for row in data]\n",
    "    avg = sum(col) / len(col)\n",
    "    print(f'{subject} 的平均成绩为：{avg:.2f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 03. 输出 scores.txt 的文件信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件名：scores.txt\n",
      "文件大小：80 字节\n",
      "文件创建时间：2026-04-17 12:16:00\n",
      "文件修改时间：2026-04-17 12:16:00\n",
      "文件最后访问时间：2026-04-17 12:16:01\n"
     ]
    }
   ],
   "source": [
    "# 任务 3：输出 scores.txt 的文件大小、创建时间、修改时间、最后访问时间\n",
    "import os\n",
    "import time\n",
    "\n",
    "file_path = 'scores.txt'\n",
    "stat_info = os.stat(file_path)\n",
    "\n",
    "print(f'文件名：{file_path}')\n",
    "print(f'文件大小：{stat_info.st_size} 字节')\n",
    "print(f'文件创建时间：{time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime(stat_info.st_ctime))}')\n",
    "print(f'文件修改时间：{time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime(stat_info.st_mtime))}')\n",
    "print(f'文件最后访问时间：{time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime(stat_info.st_atime))}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 04. 输出文件真实路径、所在目录，并删除文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scores.txt 的真实路径为：E:\\py\\代码\\scores.txt\n",
      "scores.txt 所在的目录为：E:\\py\\代码\n",
      "E:\\py\\代码\\scores.txt 是一个文件，已删除。\n",
      "删除后文件是否存在：False\n"
     ]
    }
   ],
   "source": [
    "# 任务 4：输出 scores.txt 的真实路径和所在目录，判断其是否为文件，是则删除\n",
    "import os\n",
    "\n",
    "file_path = 'scores.txt'\n",
    "real_path = os.path.realpath(file_path)\n",
    "print(f'scores.txt 的真实路径为：{real_path}')\n",
    "\n",
    "dir_path = os.path.dirname(real_path)\n",
    "print(f'scores.txt 所在的目录为：{dir_path}')\n",
    "\n",
    "if os.path.isfile(real_path):\n",
    "    os.remove(real_path)\n",
    "    print(f'{real_path} 是一个文件，已删除。')\n",
    "else:\n",
    "    print(f'{real_path} 不是一个文件。')\n",
    "\n",
    "# 验证是否删除成功\n",
    "print(f'删除后文件是否存在：{os.path.exists(real_path)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 05. 新建 Python 文件夹并重命名为 Python123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已创建文件夹：Python\n",
      "已将文件夹 Python 重命名为 Python123\n",
      "当前文件夹下 Python123 是否存在：True\n"
     ]
    }
   ],
   "source": [
    "# 任务 5：在当前文件夹下新建 Python 文件夹，并重命名为 Python123\n",
    "import os\n",
    "\n",
    "old_name = 'Python'\n",
    "new_name = 'Python123'\n",
    "\n",
    "# 若已存在同名文件夹先清理，避免重复创建报错\n",
    "if os.path.exists(old_name):\n",
    "    os.rmdir(old_name)\n",
    "if os.path.exists(new_name):\n",
    "    os.rmdir(new_name)\n",
    "\n",
    "os.mkdir(old_name)\n",
    "print(f'已创建文件夹：{old_name}')\n",
    "\n",
    "os.rename(old_name, new_name)\n",
    "print(f'已将文件夹 {old_name} 重命名为 {new_name}')\n",
    "\n",
    "print(f'当前文件夹下 {new_name} 是否存在：{os.path.exists(new_name)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 06. 判断 Python123 是否为空文件夹并删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python123 是一个空文件夹，已删除。\n",
      "删除后文件夹是否存在：False\n"
     ]
    }
   ],
   "source": [
    "# 任务 6：判断 Python123 是否为空文件夹，是则删除\n",
    "import os\n",
    "\n",
    "folder = 'Python123'\n",
    "\n",
    "if os.path.isdir(folder):\n",
    "    if len(os.listdir(folder)) == 0:\n",
    "        os.rmdir(folder)\n",
    "        print(f'{folder} 是一个空文件夹，已删除。')\n",
    "    else:\n",
    "        print(f'{folder} 不是空文件夹，未删除。')\n",
    "else:\n",
    "    print(f'{folder} 文件夹不存在。')\n",
    "\n",
    "# 验证是否删除成功\n",
    "print(f'删除后文件夹是否存在：{os.path.exists(folder)}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
