16,檔案讀寫,關閉流,with as ,

2021-10-02 02:39:50 字數 1404 閱讀 9181

做一點小改動,因為我們目前先不考慮字符集

# open(file, mode='r', buffering=-1, encoding_=none, errors=none, newline=none, closefd=true, opener=none)

# 開啟檔案

file

=open

("e:\\94_python練習\\aa.txt"

)print

(file

)

# open(file, mode='r', buffering=-1, encoding_=none, errors=none, newline=none, closefd=true, opener=none)

# 開啟檔案

file

=open

("e:\\94_python練習\\aa.txt"

)str01 =

file

.read(

)print

(str01)

# open(file, mode='r', buffering=-1, encoding_=none, errors=none, newline=none, closefd=true, opener=none)

# 開啟檔案

file

=open

("e:\\94_python練習\\aa.txt"

)str01 =

file

.read(

)print

(str01)

file

.close(

)

作用 : 對於檔案的操作,只能在 with 內部操作

** :

# open(file, mode='r', buffering=-1, encoding_=none, errors=none, newline=none, closefd=true, opener=none)

# 開啟檔案

# file = open("e:\\94_python練習\\aa.txt")

# str01 = file.read()

# print(str01)

# file.close()

with

open

("e:\\94_python練習\\aa.txt"

)as myfile:

print

(myfile.read())

print

("檔案讀取結束..."

)

例項41檔案流讀寫

命名空間 system.io 程式集 mscorlib.dll 為檔案提供 stream,既支援同步讀寫操作,也支援非同步讀寫操作。使用 filestream 類可對檔案系統中的檔案進行讀取 寫入 開啟和關閉,還可以操作其他與檔案相關的作業系統控制代碼,包括管道 標準輸入和標準輸出。您可以使用 re...

python16檔案的讀寫

open函式 open函式用來以不同的模式開啟檔案 我們來看一下這個函式的引數 open file,mode r buffering none,encoding none,errors none,newline none,closefd true 其中比較重要的是file 檔名,mode 開啟方式,...

Python語言基礎16 檔案開啟與關閉

檔案 檔案 文件 library built in functions 通過python 程式對計算機中的各種檔案進行增刪改查的操作 i o input 和 output 操作檔案的步驟 1 開啟檔案 2 對檔案進行讀寫,再儲存 3 關閉檔案 open 使用該函式來開啟乙個檔案 引數 file 要開...