標籤:新手學習 .exe 內容 nbsp rip color lin 介紹 sys.argv
檔案除r、w、a方式開啟外,還可以有多種組合方式如r+ w+ a+等多種方式
1、r+ 讀寫入模式介紹,開始讀是從一行開始讀,寫永遠從最後開始寫(類似於追加)
#f = open("test.txt","r+",encoding ="utf-8")f.readline()f.readline()f.readline()# 不管如何讀或者是seek。檔案永遠從尾部追加。寫時候,不會影響讀游標位置。print("當前游標位置:",f.tell())f.write("\n測試r+11")print("第一次插入當前游標位置:",f.tell())print(f.readline())print("當前游標位置:",f.tell())f.write("\n測試r+22")print("第二次當前游標位置:",f.tell())print(f.readline())print(f.read())E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/Users/xiajinqi/PycharmProjects/twoday/file2.py當前游標位置: 51第一次插入當前游標位置: 423追加2當前游標位置: 423第二次當前游標位置: 435追加3追加4追加5追加6追加7追加8追加9追加10測試r+測試r+測試r+測試r+測試r+測試r+測試r+測試r+測試r+測試r+11測試r+22測試r+11測試r+22測試r+11測試r+22測試r+11測試r+22測試r+11測試r+22測試r+11測試r+22測試r+11測試r+22測試r+11測試r+22
2、w+ 寫讀(建立一個新的檔案),讀完以後,游標會在尾部,讀時候需要從seek.並且讀不影響寫入到位置。在尾部寫入。
# w+相對使用較少。建立一個新的檔案讀寫f = open("test2.txt","w+",encoding=‘utf-8‘)f.write("1----------\n")f.write("2----------\n")f.write("3----------\n")print(f.readline())print(f.tell())f.seek(0)print(f.readline())f.write("test----------\n")f.seek(0)print(f.read())E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/Users/xiajinqi/PycharmProjects/twoday/file2.py391----------1----------2----------3----------test----------Process finished with exit code 0
3 a + 追加讀。不會建立新檔案。在檔案後面追加。
# w+相對使用較少。建立一個新的檔案讀寫f = open("test2.txt","a+",encoding=‘utf-8‘)f.write("1----------\n")f.write("2----------\n")f.write("3----------\n")print(f.readline())print(f.tell())f.seek(0)print(f.readline())f.write("test----------\n")f.seek(0)print(f.read())
4、rb和wb 以二進位方式讀寫。
# w+相對使用較少。建立一個新的檔案讀寫f = open("test2.txt","rb")print(f.readline())f.close()#.encode() 預設使用f = open("test2.txt","wb")print(f.write("binnary".encode()))f.close()f = open("test2.txt","rb")print(f.readline())f.close()
5、檔案修改,將檔案中的內容替換。
# 將流年未亡香樟依舊 中 將流年未亡 改為時光已逝old_file = open("test.txt",‘r‘,encoding="utf-8")new_file = open("test.txt.bak","w",encoding="utf-8")for line in old_file : if "流年未亡香樟依舊" in line : line = line.replace("流年未亡香樟依舊","時光已逝香樟依舊") new_file.write(line)old_file.close()new_file.close()歌詞內容:時光雨灑落在你我心裡消失了感情在臉上留守時光已逝香樟依舊夏天終於走到了最後多少的執著輸給了時間多少的淚水流過了青春
5、python with open("test.txt","r",encoding:"utf-8") as f 的使用
with open("test.txt",‘r‘,encoding="utf-8") as f : print(f.read())#開啟多個檔案with open("test.txt",‘r‘,encoding="utf-8") as f1 , open("test.txt",‘r‘,encoding="utf-8") as f2 : print(f.read())
6、python實現shel的替換尋找工作
# Author : xiajinqiimport sysoperotor_type = sys.arg[1]find_file = sys.argv[2]find_str = sys.argv[3]replace_str = sys.arg[4]if operotor_type == ‘f‘ : with open(find_file, ‘r+‘, encoding="utf-8") as f: for line in f: if find_str in line: print(line) else: print("尋找的內容不存在")elif operotor_type == ‘r‘ : with open(find_file, ‘r+‘, encoding="utf-8") as f1, open("test.bak", ‘w+‘, encoding="utf-8") as f2: for line in f1: if find_str in line: line =line.replace(find_file,find_str,replace_str) f2.wirte(line)else : print("usage:python find.py f/r filename findstr replacestr")
python新手學習之檔案讀寫之修改