檔案相關函式

2022-06-30 12:39:10 字數 1783 閱讀 6342

# (1)flush 

# 重新整理緩衝區 flush

# 當檔案關閉的時候自動重新整理緩衝區

# 當整個程式執行結束的時候自動重新整理緩衝區

# 當緩衝區寫滿了 會自動重新整理緩衝區

# 手動重新整理緩衝區

'''fp = open("0506_6.txt",mode="w+",encoding="utf-8")

fp.write("123456")

# 手動重新整理緩衝區

fp.flush()

while true:

pass

fp.close()

'''# (2) 檔案物件具有可迭代性

#readable() 功能: 判斷檔案物件是否可讀

#writable() 功能: 判斷檔案物件是否可寫

'''fp = open("0506_6.txt",mode="r",encoding="utf-8")

res1 = fp.readable()

res2 = fp.writable()

print(res1) #true

print(res2) #false

for i in fp:

print(i)

'''# (3) 檔案的相關函式

#readline() 功能: 讀取一行檔案內容

'''readline(字元數)

如果給的引數大於當前行字元數,只獲取當前行所有內容

如果給的引數小於當前行字元數,按照實際給的引數進行字元的獲取

''''''

with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:

res = fp.readline(300)

print(res)

# 0 0.0 0j false '' () set() {} none

# 迴圈列印檔案裡面每一行的內容

# while res:

# print(res)

# res = fp.readline()

'''

#readlines() 功能:將檔案中的內容按照換行讀取到列表當中

'''lst =

with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:

res = fp.readlines()

for i in res:

res2 = i.strip()

# print(res)

print(lst)

'''#writelines() 功能:將內容是字串的可迭代性資料寫入檔案中 引數:內容為字串型別的可迭代資料

with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:

strvar= "今天天氣好晴朗"

lst = ["親愛的\n","美女\n","請看我\n","一眼\n"]

fp.writelines(lst)

#truncate() 功能: 把要擷取的字串提取出來,然後清空內容將提取的字串重新寫入檔案中 (位元組)

with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:

fp.truncate(6)

"""read(字元)

readline(字元)

seek(位元組)

truncate(位元組)

tell返回的是位元組

"""

檔案I O相關函式

1.open 系統呼叫 開啟乙個檔案返回檔案描述符 int open const char name,int flags int open const char name,int flags,mode t mode 2.creat 函式 int creat const char name,mode ...

標頭檔案相關函式

2.6.30.4的標頭檔案的位置和2.6.25.8的不一樣,除去核心原始碼下的include目錄外,在arch arm mach s3c2410 和arch arm plat s3c24xx 目錄下都有include目錄的。include 是在linux include linux下面尋找原始檔。i...

檔案讀寫相關函式

include int creat const char path,mode t mode int open const char path,int oflag,int openat inf fd,const char path,int oflag,include int close int fd ...