with 語句python
__author__ = "Alex Li" import sys #f = open("yesterday2","r",encoding="utf-8") with open("yesterday2","r",encoding="utf-8") as f ,\ open("yesterday2", "r", encoding="utf-8") as f2: for line in f: print(line)
打印進度條緩存
# Author:Adminone import sys,time for i in range(20): sys.stdout.write("#") sys.stdout.flush() #刷新緩存文件,加快磁盤存取文件 time.sleep(0.1)
yesterday[1-3]app
Oh, yesterday when I was young 噢 昨日當我年少輕狂 So many, many songs were waiting to be sung 有那麼那麼多甜美的曲兒等我歌唱 So many wild pleasures lay in store for me 有那麼多肆意的快樂等我享受 And so much pain my eyes refused to see 還有那麼多痛苦 個人雙眼卻視而不見 There are so many songs in me that won't be sung 我有太多歌曲永遠不會被唱起 I feel the bitter taste of tears upon my tongue 我嚐到了舌尖淚水的苦澀滋味 The time has come for me to pay for yesterday 終於到了付出代價的時間 爲了昨日 When I was young 當我年少輕狂
file_op.pyide
# Author:Adminone """ data = open("yesterday",encoding="utf-8").read() print(data) """ ''' #f = open("yesterday2",'w',encoding="utf-8") #w 建立文件,清空文件數據(不建議使用),不能讀 f = open("yesterday3",'a',encoding="utf-8") #文件句柄,建立文件,不能讀 #a = append 追加 f.write("\nwhen i was young i listen to the radio\n") data = f.read() print('--read',data) f.close() ''' #high bige f = open("yesterday2",'r',encoding="utf-8") count = 0 for line in f: if count == 9: print('----我是分割線----------') count += 1 continue print(line) count +=1 ''' #low loop f = open("yesterday2",'r',encoding="utf-8") for index,line in enumerate(f.readlines()): #enumerate() 取每行的下標 if index == 9: print('----我是分割線----------') continue print(line.strip()) #清除換行符 ''' #for i in range(5): # print(f.readline()) ''' f = open("yesterday2",'r',encoding="utf-8") print(f.tell()) #print(f.readline(10)) print(f.readline()) print(f.readline()) print(f.readline()) print(f.tell()) #記錄讀取字符數 f.seek(0) #回到文件首行 print(f.readline()) f.seek(10) print(f.readline()) ''' ''' f = open("yesterday2",'r',encoding="utf-8") print(f.encoding) print(f.fileno()) #返回內存編號 print(f.seekable()) #判斷文件是否可移到首行 print(f.readable()) #True False print(f.writable()) ''' ''' f = open("yesterday3",'a',encoding="utf-8") print(f.flush()) #刷新緩存文件,加快磁盤存取文件 print(dir(f.buffer) ) #print(f.close()) #判斷文件是否關閉 #f.truncate() #文件清空 f.seek(10) f.truncate(20) #截斷 ''' ''' f = open("yesterday2",'r+',encoding="utf-8") #文件句柄 讀寫 在後面追加 print(f.readline()) print(f.readline()) print(f.readline()) f.write("-----------diao-----------") ''' ''' #f = open("yesterday2",'w+',encoding="utf-8") #文件句柄 寫讀 建立清空後寫入 #f = open("yesterday2",'a+',encoding="utf-8") #文件句柄 追加讀寫 #沒什麼卵用 f.write("\n-----------diao-----------\n") f.write("-----------diao-----------\n") f.write("-----------diao-----------\n") f.write("-----------diao-----------\n") print(f.tell()) f.seek(10) print(f.tell()) print(f.readline()) f.write("should be at thr begining of the second line") f.close() ''' ''' f = open("yesterday2",'rb') #文件句柄 二進制文件 讀 print(f.readline()) print(f.readline()) print(f.readline()) print(f.readline()) ''' ''' f = open("yesterday2",'wb') #文件句柄 二進制文件 寫 f.write("hello binary".encode("utf-8")) #字符集轉換 二進制轉字符串 f.close() ''' f = open("yesterday2",'ab') #文件句柄 二進制文件 追加 f.write("hello binary\n".encode("utf-8")) #字符集轉換 二進制轉字符串 f.close()
文件修改oop
# Author:Adminone import sys f = open("yesterday2","r",encoding="utf-8") f_new = open("yesterday2.bak","w",encoding="utf-8") # find_str = sys.argv[1] # replace_str = sys.argv[2] find_str = "肆意的快樂等我享受" replace_str = "肆意的快樂等Alex享受" for line in f: if find_str in line: line = line.replace(find_str,replace_str) f_new.write(line) f.close() f_new.close()
自動關閉打開的文件code
# Author:Adminone import sys #f = open("yesterday2","r",encoding="utf-8") with open("yesterday2","r",encoding="utf-8") as f ,\ open("yesterday2", "r", encoding="utf-8") as f2: for line in f: print(line)