python檔案操作

yepkeepmoving發表於2017-07-19
檔案操作是我們日常程式設計使用最頻繁的,其中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、with開啟和關閉檔案
語法: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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章