Python文件操作

文件操作是我们日常编程使用最频繁的,其中python对文件操作流程如下:
打开文件——>操作文件——>关闭文件 (打开文件是将得到的文件句柄赋值给变量,然后通过句柄操作文件)
一、打开文件模式
常用打开模式
  r,只读模式(默认)。
  w,只写模式。【不可读;不存在则创建;存在则删除内容;】
  a,追加模式。【可读;   不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
  r+,可读写文件。【可读;可写;可追加】
  w+,写读
  a+,追加读
"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)
  rU
  r+U
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
  rb
  wb
  ab
二、基本语法
1、打开文件
  open("filename","open_mode",encoding="encoding")
2、读取文件(打开的文件需要赋值给变量(文件句柄),后续对变量进行操作,即文件句柄的操作)
  例如:f=open("filename","open_mode",encoding="encoding")
  a=f.read()          ##一次性读取整个文件内容
  b=f.readline()     ##一次性仅读取一行
  c=f.readlines()    ##按行读取文件,读取整个文件,并以列表形式返回读取的结果
  实例:
  f.read()

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- UTF-8 -*-

  3. f=open("hero",'r',encoding="utf-8")

  4. a=f.read()
  5. print(a)

  6. ##结果如下
  7. C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe F:/python_project/Day03/function_test.py
  8. let me be you hero 让我成为你的英雄
  9. would you dance 如果我邀请你跳舞
  10. if i asked you to dance 你愿意吗
  11. would you run 还是会跑开
  12. and never look back 不再回头
  13. would you cry 如果你看到我哭泣
  14. if you saw me crying 你会哭吗
  15. and would you save my soul tonight 今夜可否拯救我的灵魂
  16. would you tremble 如果我轻触你的唇
  17. if i touched your lips 你会颤抖吗
  18. would you laugh 还是会微笑
  19. oh please tell me thisoh 请告诉我这些
  20. now would you die 你会为了你唯一的爱
  21. for the one you love 而去死吗
  22. hold me in your arms tonight 今夜请将我拥入怀中
  23. i can be your hero baby 宝贝我会成为你的英雄
  24. i can kiss away the pain 我会吻走伤痛
  25. i will stand by you forever 我会永远和你一起
  26. you can take my breath away 没有你我无法生存
  27. would you swear 你可以发誓吗
  28. that you'llalways be mine ; 你永远是我的
  29. or would you lie 还是会说谎
  30. would you run and hide 逃离我 躲避我
  31. Am i in too deep 我已深陷其中
  32. have i lost my mind 我已失去理智
  33. i don\'t care 我什么都不在乎
  34. ever since i saw your face 只要能见到你
  35. you\'re here tonight 今夜你在这里
  36. i can be your hero baby 宝贝我会成为你的英雄
  37. i can kiss away the pain 我可以吻走伤痛
  38. i will stand by you forever 我会永远和你一起
  39. you can take my breath away 没有你 我无法生存
  40. oh i just wanna to hold you 我只想拥抱你
  41. oh yeahoh yeah oh yeahoh yeahAm i in too deep 我已深陷其中
  42. have i lost my mind 我已失去理智
  43. well i don't care 我什么都不在乎
  44. you
  f.readline()

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- UTF-8 -*-

  3. f=open("hero",'r',encoding="utf-8")

  4. a=f.readline()
  5. print(a)

  6. 结果如下:
  7. C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe F:/python_project/Day03/function_test.py
  8. let me be you hero 让我成为你的英雄


  9. Process finished with exit code 0
f.readlines()

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- UTF-8 -*-

  3. f=open("hero",'r',encoding="utf-8")

  4. a=f.readlines()
  5. print(a)

  6. 结果如下:
  7. C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe F:/python_project/Day03/function_test.py
  8. ['let me be you hero 让我成为你的英雄\n', 'would you dance 如果我邀请你跳舞\n', 'if i asked you to dance 你愿意吗\n', 'would you run 还是会跑开\n', 'and never look back 不再回头\n', 'would you cry 如果你看到我哭泣\n', 'if you saw me crying 你会哭吗\n', 'and would you save my soul tonight 今夜可否拯救我的灵魂\n', 'would you tremble 如果我轻触你的唇\n', 'if i touched your lips 你会颤抖吗\n', 'would you laugh 还是会微笑\n', 'oh please tell me thisoh 请告诉我这些\n', 'now would you die 你会为了你唯一的爱\n', 'for the one you love 而去死吗\n', 'hold me in your arms tonight 今夜请将我拥入怀中\n', 'i can be your hero baby 宝贝我会成为你的英雄\n', 'i can kiss away the pain 我会吻走伤痛\n', 'i will stand by you forever 我会永远和你一起\n', 'you can take my breath away 没有你我无法生存\n', 'would you swear 你可以发誓吗\n', "that you'llalways be mine ; 你永远是我的\n", 'or would you lie 还是会说谎\n', 'would you run and hide 逃离我 躲避我\n', 'Am i in too deep 我已深陷其中\n', 'have i lost my mind 我已失去理智\n', "i don\\'t care 我什么都不在乎\n", 'ever since i saw your face 只要能见到你\n', "you\\'re here tonight 今夜你在这里\n", 'i can be your hero baby 宝贝我会成为你的英雄\n', 'i can kiss away the pain 我可以吻走伤痛 \x1e\n', 'i will stand by you forever 我会永远和你一起\n', 'you can take my breath away 没有你 我无法生存\n', 'oh i just wanna to hold you 我只想拥抱你\n', 'oh yeahoh yeah oh yeahoh yeahAm i in too deep 我已深陷其中\n', 'have i lost my mind 我已失去理智\n', "well i don't care 我什么都不在乎\n", "you're here tonight 今夜只要你在这里\n", 'i can be your hero baby 宝贝我会成为你的英雄\n', 'i can kiss away the pain 我可以吻走伤痛\n', 'i will stand by you forever 我会永远和你一起\n', 'you can take my breath away 没有你 我无法生存 \x1c\n', 'i can be your hero No \x1d\x1d 我会成为你的英雄\n', 'i can kiss away the pain 我可以吻走伤痛\n', 'and i will stand by you forever 我会永远和你一起\n', 'you can take my breath away 没有你 我无法生存\n', 'you can take my breath 没有你 我无法生存\n', 'i can be your hero 我会成为你的英雄']

  9. Process finished with exit code 0
读取前几行:

  1. f=open("hero",'r',encoding="utf-8")

  2. for i in range(5):
  3.     print(f.readline())
按行读取整个文件:
方式一

  1. f=open("hero",'r',encoding="utf-8")

  2. for i in f.readlines():
  3.     print(i.strip())
方式二

  1. f=open("hero",'r',encoding="utf-8")

  2. for i in f:
  3.     print(i.strip())

3 文件
  f.write()
(下面文件写入不会换行,如果需要换行需要在写结束假如\n换行符)

点击(此处)折叠或打开

  1. f=open("writefile",'w',encoding="utf-8")

  2. f.write("写模式写入文件")
  3. f.write("------------")
上面打开模式是w,只允许写入文件,不允许读取文件,而且此模式文件不存在则创建,文件存在的话覆盖文件源内容,相当于>文件内容
      打开模式是a,则写入文件是追加模式,相当于>>文件内容,此模式也不允许读,文件不存在也是创建文件
列表下标读取:

  1. for i,l in enumerate(f.readlines()):
  2.     if i==9:
  3.         print("========")
  4.         continue
  5.     print(l.strip())
f.tell()
  打印出文件的光标位置,按字符计算
f.seek()
  将光标回到文件的某个位置,按字符计算,回到最初是seek(0)
4、 文件

5、 关闭 文件
  f.close()

6、 w ith 打开和关闭文件
语法:with open(filename1,open_mode,encoding) as file1,open(filename2,open_mode,encoding) as file2:
                operation

点击(此处)折叠或打开

  1. with open("hero",'r',encoding="utf-8") as f:
  2.     for i in f:
  3.         print(i.strip())
三、

四、附录
1、文件附录

点击(此处)折叠或打开

  1. let me be you hero 让我成为你的英雄
  2. would you dance 如果我邀请你跳舞
  3. if i asked you to dance 你愿意吗
  4. would you run 还是会跑开
  5. and never look back 不再回头
  6. would you cry 如果你看到我哭泣
  7. if you saw me crying 你会哭吗
  8. and would you save my soul tonight 今夜可否拯救我的灵魂
  9. would you tremble 如果我轻触你的唇
  10. if i touched your lips 你会颤抖吗
  11. would you laugh 还是会微笑
  12. oh please tell me thisoh 请告诉我这些
  13. now would you die 你会为了你唯一的爱
  14. for the one you love 而去死吗
  15. hold me in your arms tonight 今夜请将我拥入怀中
  16. i can be your hero baby 宝贝我会成为你的英雄
  17. i can kiss away the pain 我会吻走伤痛
  18. i will stand by you forever 我会永远和你一起
  19. you can take my breath away 没有你我无法生存
  20. would you swear 你可以发誓吗
  21. that you'llalways be mine ; 你永远是我的
  22. or would you lie 还是会说谎
  23. would you run and hide 逃离我 躲避我
  24. Am i in too deep 我已深陷其中
  25. have i lost my mind 我已失去理智
  26. i don\'t care 我什么都不在乎
  27. ever since i saw your face 只要能见到你
  28. you\'re here tonight 今夜你在这里
  29. i can be your hero baby 宝贝我会成为你的英雄
  30. i can kiss away the pain 我可以吻走伤痛
  31. i will stand by you forever 我会永远和你一起
  32. you can take my breath away 没有你 我无法生存
  33. oh i just wanna to hold you 我只想拥抱你
  34. oh yeahoh yeah oh yeahoh yeahAm i in too deep 我已深陷其中
  35. have i lost my mind 我已失去理智
  36. well i don't care 我什么都不在乎
  37. you're here tonight 今夜只要你在这里
  38. i can be your hero baby 宝贝我会成为你的英雄
  39. i can kiss away the pain 我可以吻走伤痛
  40. i will stand by you forever 我会永远和你一起
  41. you can take my breath away 没有你 我无法生存
  42. i can be your hero No 我会成为你的英雄
  43. i can kiss away the pain 我可以吻走伤痛
  44. and i will stand by you forever 我会永远和你一起
  45. you can take my breath away 没有你 我无法生存
  46. you can take my breath 没有你 我无法生存
  47. i can be your hero 我会成为你的英雄



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27067062/viewspace-2142318/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/27067062/viewspace-2142318/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值