import os def replace(filePath, w2u): try: oldfile = open(filePath, "rb+") #這里必須用b打開 path, name = os.path.split(filePath) newfile = open(path + '$' + name, "ba+") old = b'' new = b'' if w2u == True: old = b' ' new = b'' else: old = b' ' new = b' ' data = b'' while (True): data = oldfile.read(200) newData = data.replace(old, new) newfile.write(newData) if len(data) < 200: break newfile.close() oldfile.close() os.remove(filePath) os.rename(path + '$' + name, filePath) except IOError as e: print(e) if __name__ == "__main__": print("請輸入文件路徑:") filePath = input() replace(filePath, False) #這個改為True就可以實現 變成
要注意的是,在python里,像 這樣的符號,如果是文本打開的話,是找不到 的,而只能找到' ',所以必須用b(二進制)模式打開。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com