Python基礎階段:檔案基本操作

來源:互聯網
上載者:User

標籤:組成   列表   操作   from   長度   display   檔案基本操作   opened   清除   


  1 # 1. 開啟檔案  2   3 # 相對路徑, 相對於哪一個目錄下面的指定檔案  4 # f = open("a.txt", "r+")  5 #  6 # # 2. 讀寫操作  7 # content = f.read()  8 # print(content)  9 # # 10 # f.write("88888") 11 # 12 # 13 # # 3. 關閉檔案 14 # f.close() 15  16  17  18  19 # 1. 開啟xx.jpg檔案, 取出內容, 擷取內容的前面半部分 20 # 1.1 開啟檔案 21 # fromFile = open("xx.jpg", "rb") 22 # 23 # # 1.2 讀取檔案內容 24 # fromContent = fromFile.read() 25 # print(fromContent) 26 # 27 # # 1.3 關閉檔案 28 # fromFile.close() 29 # 30 # 31 # # 2. 開啟另外一個檔案xx2.jpg, 然後, 把取出的半部分內容, 寫入到xx2.jpg檔案裡面去 32 # # 2.1 開啟目標檔案 33 # toFile = open("xx2.jpg", "wb") 34 # 35 # # 2.2 寫入操作 36 # content = fromContent[0: len(fromContent) // 2] 37 # toFile.write(content) 38 # 39 # 40 # # 2.3 關閉檔案 41 # toFile.close() 42  43  44  45 # f = open("a.txt", "rb") 46 # 47 # print(f.tell()) 48 # f.seek(-2, 2) 49 # print(f.tell()) 50 # 51 # print(f.read()) 52 # print(f.tell()) 53 # 54 # 55 # 56 # f.close() 57  58 # f = open("a.txt", "r") 59  60 # f.read(位元組數) 61 #     位元組數預設是檔案內容長度 62 #     下標會自動後移e 63 # f.seek(2) 64 # content = f.read(2) 65 # print(f.tell()) 66 # print(content) 67  68  69  70 # f.readline([limit]) 71 #     讀取一行資料 72 #     limit 73 #         限制的最大位元組數 74 # print("----", f.tell()) 75 # content = f.readline() 76 # print(content, end="") 77 # print("----", f.tell()) 78 # 79 # 80 # 81 # content = f.readline() 82 # print(content, end="") 83 # 84 # 85 # print("----", f.tell()) 86 # 87 # content = f.readline() 88 # print(content, end="") 89 # 90 # print("----", f.tell()) 91  92  93  94 # f.readlines() 95 #     會自動的將檔案按分行符號進行處理 96 #     將處理好的每一行組成一個列表返回 97  98 # content = f.readlines() 99 # print(content)100 #101 #102 # f.close()103 104 105 import collections106 107 108 # f = open("a.txt", "r")109 110 # print(isinstance(f, collections.Iterator))111 #112 # for i in f:113 #     print(i, end="")114 #115 # if f.readable():116 #     content = f.readlines()117 #     for i in content:118 #         print(i, end="")119 120 # f.close()121 122 # f = open("a.txt", "r")123 #124 # if f.writable():125 #     print(f.write("abc"))126 #127 #128 # f.close()129 130 131 132 f = open("a.txt", "w")133 134 135 f.write("123")136 137 138 f.flush()   #即刻關閉,清除緩衝139 140 f.close()
 1 import os 2  3 # os.rename("b.txt", "bb.txt") 4 # os.rename("first", "one") 5  6  7  8 # os.rename("one/one.txt", "two/two.txt") 9 10 # os.renames("one/one.txt", "two/two.txt")   #樹狀修改11 12 13 14 15 16 17 18 # os.remove("xx2.jpg")19 20 21 22 # os.rmdir("one/one2")23 24 25 # os.removedirs("one/one2")   #遞迴刪除26 27 28 29 30 31 32 33 #建立目錄34 # os.mkdir("a")35 # os.mkdir("b/c/d")    #不支援建立多級目錄36 37 38 # os.mkdir("b", 0o777)39 40 # os.chdir("a")41 #42 #43 # open("dd.txt", "w") # open("目錄/dd.txt", "w")44 45 46 # 擷取目前的目錄47 #     os.getcwd()48 49 # print(os.getcwd())50 # 改變預設目錄51 #     os.chdir("目標目錄")52 # 擷取目錄內容列表53 #     os.listdir("./")54 55 56 # print(os.listdir("a"))57 # print(os.listdir("../"))
檔案相關操作

 

 

Python基礎階段:檔案基本操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.