''''' Created on Dec 18, 2012 寫(xiě)入文件 @author: liury_lab ''' # 最簡(jiǎn)單的方法 all_the_text = 'hello python' open('d:/text.txt', 'w').write(all_the_text) all_the_data = b'abcd1234' open('d:/data.txt', 'wb').write(all_the_data) # 更好的辦法 file_object = open('d:/text.txt', 'w') file_object.write(all_the_text) file_object.close() # 分段寫(xiě)入 list_of_text_strings = ['hello', 'python', 'hello', 'world'] file_object = open('d:/text.txt', 'w') for string in list_of_text_strings: file_object.writelines(string) list_of_text_strings = ['hello', 'python', 'hello', 'world'] file_object = open('d:/text.txt', 'w') file_object.writelines(list_of_text_strings)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com