1. 程式人生 > >python 基礎復習 08 之文件操作及練習

python 基礎復習 08 之文件操作及練習

readlines 絕對路徑 www += 數據結構與算法 break open and log

  1 # 絕對路徑: 就是最完整的路徑  例子:"E:\數據結構與算法\python.txt"
  2 # 相對路徑: 相對則是不完整路徑。也就是說咱們寫的相對路徑必須是當前文件夾裏的文件才可以open。
  3 
  4 #  只讀 :r
  5 #        rb
  6 # f = open(‘模特主婦老師‘, mode=‘r‘, encoding=‘utf-8‘)
  7 # content = f.read()
  8 # print(content)
  9 # f.close()
 10 
 11 #  在r+模式下 的先讀 後寫
 12 # f = open(‘log‘, mode=‘r+‘, encoding=‘utf-8‘)
13 # print(f.read()) 14 # f.write(‘小狼,大狼‘) 15 # f.close() 16 17 # 在 r+ 模式下的 先寫 後讀 18 # 寫多少只會占多少,不會全部改動 19 # f = open(‘log‘, mode=‘r+‘, encoding=‘utf-8‘) 20 # f.write(‘BBBBB‘) 21 # print(f.read()) 22 # f.close() 23 24 25 # 註:只要open裏面有‘b’,那麽就不需要encoding=‘utf-8‘。 26 # 但是 得在 f.write(‘WWWWWw‘.encode(‘utf-8‘)) 向這樣寫
27 # f = open(‘log‘, mode=‘r+b‘) 28 # print(f.read()) 29 # f.write(‘WWWWWw‘.encode(‘utf-8‘)) 30 # f.close() 31 # 如果是字母 bytes 內部寫得是0101010101.... 32 # 如果是中文, bytes 內部是以16進制存儲 33 34 # r+ 讀寫 35 # r+b 讀寫(以bytes類型) 36 37 # 只寫:w 38 # wb 39 40 # 先將原文件的內容全部清除,然後在寫。 41 # f = open(‘log‘, mode=‘w‘, encoding=‘utf8‘)
42 # f.write("老鷹變雄鷹") 43 # f.close() 44 45 # f = open(‘log‘, mode=‘wb‘) # bytes 類型 46 # f.write("老鷹變雄鷹".encode(‘utf-8‘)) 47 # f.close() 48 49 # w+ 與 w 幾乎一樣 都是先刪除,在寫。 50 # f = open(‘log‘, mode=‘w+‘, encoding=‘utf-8‘) 51 # f.write(‘我愛編程!‘) 52 # print(f.read()) 53 # f.close() 54 55 # a 追加 56 # f = open(‘log‘, mode=‘a‘, encoding=‘utf-8‘) 57 # f.write(‘wws‘) 58 # f.close() 59 60 # f = open(‘log‘, mode=‘ab‘) 61 # f.write(‘lizywws‘.encode(‘utf-8‘)) 62 # f.close() 63 64 # f = open(‘log‘, mode=‘a‘, encoding=‘utf-8‘) 65 # f.write(‘lizy‘) 66 # f.read() # 報錯 一種模式 只能進行一種模式的操作 67 # f.close() 68 69 70 # f = open(‘log‘, mode=‘a+‘, encoding=‘utf-8‘) 71 # f.write("灰熊") 72 # f.seek(0) # f.seek(0) 可以讀出 73 # print(f.read()) 74 # f.close() 75 76 # ‘+‘ 加號 可以多執行一個動作 77 78 # 功能詳解 79 # f = open(‘log‘, mode=‘r+‘, encoding=‘utf-8‘) 80 # content = f.read(3) # read 讀出來的都是字符 81 # print(content) 82 # f.close() 83 84 # f = open(‘log‘, mode=‘r+‘, encoding=‘utf-8‘) 85 # # f.tell() 告訴你光標的位置 # 另一個知識點 (斷點續傳) 86 # print(f.tell()) # 先告訴我光標的位置,然後再用seek定光標的位置 87 # f.seek(3) # seek 是按照字節定光標的位置 88 # # content = f.read() 89 # # print(content) 90 # f.close() 91 92 # 得先知道光標有多少位 用tell, 然後用seek 93 # 去尋找 減是向左 跟橫坐標一樣 94 # f = open(‘log‘, mode=‘a+‘, encoding=‘utf-8‘) 95 # f.write(‘蜘蛛‘) 96 # count = f.tell() 97 # f.seek(count-9) 98 # print(f.read(2)) # read裏可以加參數 數字 讀多少 99 # f.close() 100 101 # readline 102 # f = open(‘log‘, mode=‘r+‘, encoding=‘utf-8‘) 103 # line = f.readline() # 一行一行讀 104 # print(line) 105 # f.close() 106 107 # readlines 108 # f = open(‘log‘, mode=‘r+‘, encoding=‘utf-8‘) 109 # line1 = f.readlines() # 返回列表 每一行當成列表中的一個元素,添加到list中 110 # print(line1) 111 # f.close() 112 113 # truncate 對原文件進行截取 114 # f = open(‘log‘, mode=‘r+‘, encoding=‘utf-8‘) 115 # l1 = f.truncate(9) 116 # f.close() 117 118 # 把用戶名和密碼寫入文件中,判斷用戶名與密碼用文件中的來判斷,有三次輸入機會 119 # username = input("請註冊用戶名:") 120 # password = input("請輸入註冊密碼:") 121 # with open("list_of_info", mode="w", encoding="utf-8") as f: 122 # f.write(‘{}\n{}‘.format(username,password)) 123 # print("恭喜你,註冊成功!") 124 # lis = [] 125 # i = 0 126 # while i < 3: 127 # uname = input("請輸入您的用戶名:") 128 # upasswd = input("請輸入您的登陸密碼:") 129 # with open("list_of_info", mode="r+", encoding="utf-8") as f1: 130 # for line in f1: 131 # lis.append(line) 132 # if uname == lis[0].strip() and upasswd == lis[1].strip(): 133 # print("登陸成功!") 134 # break 135 # else:print("賬號或密碼錯誤!請重新輸入") 136 # i += 1

# 註:本文是根據老男孩課程內容整理而成的,本文僅供個人筆記使用,如果有侵犯,請聯系我,我立即撤銷。
 

python 基礎復習 08 之文件操作及練習