文件操作.py

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一.文件的类型(编码不同分类)\n",
    "    1.文本文件:基于字符编码, 存储的是普通字符串, 不包括字体,字号,样式,颜色等信息\n",
    "        可通过文本编辑器显示和编辑。如.txt/.py/.html/.csv/.xls/.xlsx\n",
    "    2.二进制文件:基于值编码的 ,以字节形式存储,其编码长度根据值的大小长度可变。\n",
    "        如.jpg/.avi等图片,音频或视频文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二.文件的操作\n",
    "    1.使用open()函数打开(或建立)文件,返回一个file对象;\n",
    "    2.使用file对象的读/写方法对文件进行读/写的操作;\n",
    "    3.使用file对象的close()方法关闭文件。\n",
    "    4.示例如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('../Stu_pack/See You Again.txt') #使用open()函数打开指定路径的文件,返回一个f对象;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"See You Again- Wiz Khalifa&Charlie Puth\\nIt's Been A Long DayWithout You My Friend.\\nAnd I'll Tell You AllAbout It When I See You Again.\\nWe've Come A Long WayFrom Where We Began.\\nOH I'll Tell You AllAbout It When I See You Again.\\nWhen I See You Again~\\n(RAP)\\nDamn\\nWho Knew\\nAll The Planes We Flew\\nGood Things We BeenThrough\\nThat I'd Be StandingRight Here Talking To You About\\nAnother Path I Know WeLoved To Hit The Road And Laugh\\nBut Something Told MeThat It Wouldn't Last\\nHad To Switch Up\\nLook At Things DifferentSee The Bigger Picture\\nThose Were The Days HardWork Forever Pays\\nNow I See You In A BetterPlace.\\nHow Could We Not TalkAbout Family When Family's All That We Got?\\nEverything I Went ThroughYou Were Standing There By My Side\\nAnd Now You Be With GonnaMe For The Last Ride.\\nIt's Been A Long DayWithout You My Friend.\\nAnd I'll Tell You AllAbout It When I See You Again.\\nWe've Come A Long WayFrom Where We Began.\\nOH I'll Tell You AllAbout It When I See You Again.\\nWhen I See You Again~\\n(RAP)\\nFirst You Both Go OutYour Way\\nAnd The Vibe Is FeelingStrong\\nAnd What's Small Turn ToA friendship\\nTurned Into A Bond AndThat Bond Will\\nNever Be Broke And TheLove Will\\nNever Get Lost.\\nAnd When Brotherhood Come\\nFirst Then The Line WillNever Be Crossed\\nEstablished It On Our Own\\nWhen That Line Had To BeDrawn\\nAnd That Line Is\\nWhat We Reached SoRemember Me When I'm Gone.\\nHow Could We Not TalkAbout Family When Family's All That We Got?\\nEverything I Went ThroughYou Were Standing There By My Side\\nAnd Now You Be With GonnaMe For The Last Ride.\\nLight Guide Your Way.\\nHold Every Memory As YouGo\\nAnd Every Road You Take\\nO Will Always Lead YouHome.\\nIt's Been A Long DayWithout You My Friend.\\nAnd I'll Tell You AllAbout It When I See You Again.\\nWe've Come A Long WayFrom Where We Began.\\nOH I'll Tell You AllAbout It When I See You Again.\\nWhen I See You Again~\""
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.read() #使用f对象的方法read()读取内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.close() #使用f对象的方法close()关闭文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('../Stu_pack/Dream It Possible.txt','rt',encoding = 'utf-8') #'utf-8'中间是杆不是下划线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Dream It Possible(梦想成为可能)- Delacey 歌词(中英对照)\\n\\n作词:Andy Love    \\n\\n作曲:Andy Love\\n\\nI will run I will climb I will soar.\\n\\n 我奔跑,我攀爬,我要飞翔。\\n\\nI'm undefeated. \\n\\n我所向披靡。\\n\\nJumping out of my skin pull the chord. \\n\\n跳出我的桎梏,拨弄琴弦。\\n\\nYeah I believe it. \\n\\n是啊,我坚信。\\n\\nThe past is everything we were don't make us who we are. \\n\\n历史决定曾经,但并不能决定现在的你和我。\\n\\nso I'll dream until I make it real and all I see is stars. \\n\\n所以我会不懈梦想直到梦变成真,直到窥见漫天星光。\\n\\nIt's not until you fall that you fly. \\n\\n不再畏惧跌倒,直到你展翅高飞的那一刻。\\n\\nWhen your dreams come alive you're unstoppable. \\n\\n当梦想成真,你便无可阻挡。\\n\\ntake a shot chase the sun, find the beautiful. \\n\\n挥着双翅,追随太阳,寻找美好。\\n\\nWe will glow in the dark turning dust to gold. \\n\\n我们会在黑暗中耀发光芒,点石成金。\\n\\nAnd we'll dream it possible. \\n\\n我们期盼梦想成真。\\n\\nPossible. \\n\\n梦想成真。\\n\\nAnd we’ll dream it possible.\\n\\n我们期盼梦想成真。\\n\\nI will chase I will reach I will fly. \\n\\n我追随,我向前,我会展翅翱翔。\\n\\nUntil I'm breaking until I'm breaking. \\n\\n直到跌倒,直到毁灭的那一刻。\\n\\nOut of my cage like a bird in the night. \\n\\n走出自我的牢笼,像黑夜中自由高飞的鸟儿般。\\n\\nI know I'm changing I know I'm changing. \\n\\n我知道我在蜕变,在蜕变。\\n\\nIn into something big better than before. \\n\\n变成未曾有过的强大。\\n\\nAnd if it takes takes a thousand lives. \\n\\n崇高理想的实现。\\n\\nThen it's worth fighting for. \\n\\n总是需要付出无数鲜血。\\n\\nIt's not until you fall that you fly. \\n\\n不再畏惧跌倒,直到你展翅高飞的那一刻。\\n\\nWhen your dreams come alive you're unstoppable. \\n\\n当梦想成真,你便无可阻挡。\\n\\ntake a shot chase the sun find the beautiful. \\n\\n挥着双翅,追随太阳,寻找美好。\\n\\nWe will glow in the dark turning dust to gold. \\n\\n我们会在黑暗中耀发光芒,点石成金。\\n\\nAnd we'll dream it possible. \\n\\n我们期盼梦想成真。\\n\\nIt possible. \\n\\n梦想成真。\\n\\nFrom the bottom to the top. \\n\\n从低谷到巅峰。\\n\\nWe're sparking wild fire's. \\n\\n我们犹如燎燃的野火。\\n\\nNever quit and never stop. \\n\\n永不放弃,永不退缩。\\n\\nThe rest of our lives. \\n\\n穷尽余生,不懈奋斗。\\n\\nFrom the bottom to the top. \\n\\n从低谷到巅峰。\\n\\nWe're sparking wild fire's. \\n\\n我们犹如燎燃的野火。\\n\\nNever quit and never stop. \\n\\n永不放弃,永不退缩。\\n\\nIt’s not until you fall that you fly. \\n\\n不再畏惧跌倒,直到你展翅高飞的那一刻。\\n\\nWhen your dreams come alive you're unstoppable. \\n\\n当梦想成真,你便无可阻挡。\\n\\ntake a shot chase the sun find the beautiful. \\n\\n挥着双翅,追随阳光,寻找美好。\\n\\nWe will glow in the dark turning dust to gold. \\n\\n我们会在黑暗中耀发光芒,点石成金。\\n\\nAnd we'll dream it possible. \\n\\n我们期盼梦想成真。\\n\\nPossible. \\n\\n梦想成真。\\n\\nAnd we'll dream it possible. \\n\\n我们期盼梦想成真。\""
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = f.readlines()\n",
    "for line in file: #读一行打印一行,直到读完\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值