與小卡特一起學python 第22章 檔案輸入與輸出
#22-1 開啟和讀檔案
my_file = open('notes.txt','r')
lines = my_file.readlines()
print(lines)
my_file.close()
#22-2 多次使用readline()
my_file = open('notes.txt','r')
first_line = my_file.readline()
second_line = my_file.readline()
print("first line = ",first_line)
print("second line = ",second_line)
my_file.close()
#22-21 seek()
my_file = open('notes.txt','r')
first_line = my_file.readline()
second_line = my_file.readline()
print("first line = ",first_line)
print("second line = ",second_line)
my_file.seek(5)
first_line_again = my_file.readline()
print(first_line_again)
my_file.close()
#22-3使用追加模式
todo_list = open("notes.txt","a")
todo_list.write ("\nSpeed allowance")
todo_list.close()
#22-4 對一個新檔案使用寫模式
new_file = open("my_new_notes.txt","w")
new_file.write("Eat supper\n")
new_file.write("Play soccer\n")
new_file.write("Go to bed")
new_file.close()
#22-5 對一個現有檔案使用寫模式
the_file = open("notes.txt","w")
the_file.write("Wake up\n")
the_file.write("Watch cartoons")
the_file.close()
my_file = open('notes.txt','r')
lines = my_file.readlines()
print(lines)
my_file.close()
#22-6 使用pickle將列表儲存到檔案中
import pickle
my_list = ['Fred',73,'Hello there',8.19876e-13]
pickle_file = open('my_pickled_list.pkl','wb')#wb二進位制儲存 與書中不一樣
pickle.dump(my_list,pickle_file,1)
pickle_file.close()
#22-7 使用load()還原
import pickle
pickle_file = open('my_pickled_list.pkl','rb')#二進位制開啟
recovered_list = pickle.load(pickle_file)
pickle_file.close()
print(recovered_list)
my_file = open('notes.txt','r')
lines = my_file.readlines()
print(lines)
my_file.close()
#22-2 多次使用readline()
my_file = open('notes.txt','r')
first_line = my_file.readline()
second_line = my_file.readline()
print("first line = ",first_line)
print("second line = ",second_line)
my_file.close()
#22-21 seek()
my_file = open('notes.txt','r')
first_line = my_file.readline()
second_line = my_file.readline()
print("first line = ",first_line)
print("second line = ",second_line)
my_file.seek(5)
first_line_again = my_file.readline()
print(first_line_again)
my_file.close()
#22-3使用追加模式
todo_list = open("notes.txt","a")
todo_list.write ("\nSpeed allowance")
todo_list.close()
#22-4 對一個新檔案使用寫模式
new_file = open("my_new_notes.txt","w")
new_file.write("Eat supper\n")
new_file.write("Play soccer\n")
new_file.write("Go to bed")
new_file.close()
#22-5 對一個現有檔案使用寫模式
the_file = open("notes.txt","w")
the_file.write("Wake up\n")
the_file.write("Watch cartoons")
the_file.close()
my_file = open('notes.txt','r')
lines = my_file.readlines()
print(lines)
my_file.close()
#22-6 使用pickle將列表儲存到檔案中
import pickle
my_list = ['Fred',73,'Hello there',8.19876e-13]
pickle_file = open('my_pickled_list.pkl','wb')#wb二進位制儲存 與書中不一樣
pickle.dump(my_list,pickle_file,1)
pickle_file.close()
#22-7 使用load()還原
import pickle
pickle_file = open('my_pickled_list.pkl','rb')#二進位制開啟
recovered_list = pickle.load(pickle_file)
pickle_file.close()
print(recovered_list)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2088972/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python:檔案的輸入與輸出Python
- 瞭解下C# 檔案的輸入與輸出C#
- 第10章 對檔案的輸入輸出
- Python資料的輸入與輸出Python
- 排序,檔案輸入輸出排序
- 第九章:輸入/輸出流與檔案操作 習題
- 06Numpy輸入與輸出
- 物聯網學習教程——格式輸入與輸出
- linux中的輸入與輸出管理(重定向輸入,輸出,管道符)Linux
- AUTOCAD——圖形的輸入與輸出
- 新手學python之Python的輸入輸出函式Python函式
- 檔案輸入輸出處理(二)-位元組流
- python ----輸入輸出 變數Python變數
- Python輸入和輸出(IO)Python
- python 標準輸入輸出Python
- 第4周 4.2 簡單輸入輸出
- ACM的Python版輸入輸出ACMPython
- converter設計模式擴充套件,多種輸入輸出與標準輸入輸出的轉化方案設計模式套件
- Python教程:7. 輸入和輸出Python
- python基礎篇-輸入和輸出Python
- Linux 下的檔案管理&管理系統中的輸出輸入Linux
- 分治法求眾數和重數(含檔案輸入輸出)
- 使用Java NIO 和 NIO2實現檔案輸入/輸出Java
- 輸入輸出
- 基本的python知識 (輸入和輸出)Python
- printf與scanf如何輸出、輸入十六進位制與八進位制數
- pat處理輸入輸出小技巧(待更新)
- 專案整合管理主要輸入、工具、輸出
- python怎麼將列印輸出日誌檔案Python
- 輸入輸出流
- 重學java中的輸入輸出流Java
- 檔案輸入和輸入出處理(六)-序列化和反序列化
- MySQL輸出html格式檔案MySqlHTML
- C語言log日誌管理-支援檔案與終端輸出C語言
- C語言檔案輸入和輸出操作的學習心得(一)C語言
- MNN模型輸出與ONNX模型輸出對不上模型
- [20190524]sqlplus 與輸出&.txtSQL
- Java 輸入輸出流Java
- 1.輸入輸出