python3.7 文件操作


#!/usr/bin/env python 
__author__ = "lrtao2010" 

#python3.7 文件操作


# r   只讀,默認打開方式,當文件不存在時會報錯
# w   只寫,當文件不存在時會自動創建文件,文件內容只能是字符串,只能寫入字符串
# r+  可讀可寫,當文件不存在時會報錯
# w+  可讀可寫。當文件不存在時會新建
# a   追加文件,不可讀
# a+  追加文件,可讀可寫
# rb  以二進制讀模式打開,只可讀
# rb+ 以二進制寫讀寫模式打開,可讀可寫,當文件不存在時報錯
# wb  以位進制寫模式打開,只可寫
# wb+ 以二進制讀寫模式打開,可讀可寫。當文件不存在時新建
# ab  以二進制追加模式打開,追加文件,不可讀
# ab+ 以二進制讀寫模式打開,追加文件。可讀可寫

# with open("file.txt",'w',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     False
#     True
# with open("file.txt",'r',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     True
#     False
# with open("file.txt",'r+',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     True
#     True
# with open("file.txt",'a',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     False
#     True
# with open("file.txt",'a+',encoding='utf-8') as f:
#     print(f.readable())
#     print(f.writable())
#     True
#     True

# f= open("file1.txt",encoding='utf-8')#不寫encoding,默認問操作系統編碼
# file_data=f.read() #完全讀取
# print(file_data)
# f.close() #需要執行關閉操作
# 1111
# 222
# 333
# 44
# 5555
# 666
# 你好!
# hello

# with open("file.txt",encoding='utf-8')as f:
#     file_data=f.read()
#     print(file_data)  #不需要執行close(),系統會自動關閉。
# 1111
# 222
# 333
# 44
# 5555
# 666
# 你好!
# hello

# with open("file.txt",encoding='utf-8')as f:
#     print(f.readable()) #判斷是否可讀
#     print(f.writable()) #判斷是否可寫
#     print(1, f.readline()) #一次只讀一行
#     print(2, f.readline())
#     print(3, f.readline())
#     print(4, f.readline())
#     print(5, f.readline())
#     print(6, f.readline())
#     print(7, f.readline())
#     print(8, f.readline())
# True
# False
# 1 1111
#
# 2 222
#
# 3 333
#
# 4 44
#
# 5 5555
#
# 6 666
#
# 7 你好!
#
# 8 hello


#with open("file.txt",encoding='utf-8')as f:
#    print(f.readlines()) #將文件內容讀取到列表中
#   ['1111\n', '222\n', '333\n', '44\n', '5555\n', '666\n', '你好!\n', 'hello']
#     for i in f.readlines():
#         print(i)
#         1111
#
#         222
#
#         333
#
#         44
#
#         5555
#
#         666
#
#         你好!
#
#         hello

# with open('file.txt','r',encoding='utf-8',newline='') as f:
# #     print(f.readlines())
# #     ['1111\r\n', '222\r\n', '333\r\n', '44\r\n', '5555\r\n', '666\r\n', '你好!\r\n', 'hello']
#     for i in f.readlines():
#         print(i,end='') #不打印換行符
#         1111
#         222
#         333
#         44
#         5555
#         666
#         你好!
#         hello

# with open('file.txt','w+',encoding='utf-8') as f: #原文件被清空后重新寫入
#     f.write('a\n')
#     f.write('b\nc\n')
#     f.writelines(['d\n', 'e\n'])
#     f.seek(0) #將指針seek到0位置,否則讀不出數據
#     print(f.read())
#     # a
#     # b
#     # c
#     # d
#     # e

# with open('file.txt','r+',encoding='utf-8') as f: #從指針位置所在處寫入寫入
#     print(f.readline())
#     f.write('a\n')
#     f.write('b\nc\n')
#     f.writelines(['d\n', 'e\n'])
#     f.seek(0) #將指針seek到0位置,否則讀不出數據
#     print(f.read())
#     # 1111
#     #
#     # 1111
#     # 222
#     # 333
#     # 44
#     # 5555
#     # 666
#     # 你好!
#     # helloa
#     # b
#     # c
#     # d
#     # e

# with open('file.txt','a+',encoding='utf-8') as f:
#     f.write('寫到文件最后')
#     f.seek(0)
#     print(f.read())
#     # 1111
#     # 222
#     # 333
#     # 44
#     # 5555
#     # 666
#     # 你好!
#     # hello寫到文件最后

# with open('file.txt','a+',encoding='utf-8') as f:
#     print(f.encoding) #查看文件編碼
#     #utf - 8

# #'字符串'---------encode---------》bytes
# #bytes---------decode---------》'字符串'

# with open('file.txt','rb') as f: #b模式不能指定編碼
#     file_data=f.read()
#     print(file_data)
#     print(file_data.decode('utf-8'))
#     # b'1111\r\n222\r\n333\r\n44\r\n5555\r\n666\r\n\xe4\xbd\xa0\xe5\xa5\xbd!\r\nhello'
#     # 1111
#     # 222
#     # 333
#     # 44
#     # 5555
#     # 666
#     # 你好!
#     # hello

# with open('file.txt','wb+') as f:
#     file_data = 'test wb'
#     f.write(file_data.encode('utf-8'))
#     f.seek(0)
#     print(f.read())
#     # b'test wb'

# flush() 文件內容從內存刷到硬盤
# tell()  查看文件當前光標位置
# seek(3) #從開頭開始算,將光標移動到第三個字節
          #seek 有三種工作方式,seek(offset[, whence])
          #seek(2,0)=seek(2),0是默認方式,相當於從0字節位置開始
          #seek(2,1)   1 相對當前位置
          #seek(-2,2)  2 從文件末尾開始
# truncate(10) #從開頭開始算,將文件只保留從0-10個字節的內容,文件打開方式必須包含"寫",
               #但是w和w+除外,因為這兩種方式會首先把文件清空。
# with open('file.txt','ab') as f:
#     f.truncate(1)


#打印文件最后一行
# with open("file.txt",'rb') as f:
#     for i in f:   #這種方式不會讀取整個文件,需要從哪里讀取才從哪里開始讀取,循環文件的推薦方式
#         offs=-5         #偏移量,根據一行大小確定
#         while True:
#             f.seek(offs,2)
#             data=f.readlines()
#             if len(data) > 1:
#                 print('這是最后一行:',data[-1].decode('utf-8'))
#                 break
#             offs*=2

# 這是最后一行: hello你好!hello你好!hello你好!hello你好!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM