1. Mở, đọc file và in nội dung file ra màn hình - Read
2. Mở và tự động đóng file sau khi đọc
3. Tạo và ghi file - Write
4. Ghi thêm dữ liệu vào file - Append
Thực hiện:
1. Mở, đọc file và in nội dung file ra màn hình - Read
- Soạn ile DATA.txt có nội dung như dưới:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 192.168.122.11 YES NVRAM up up
Serial0/0 unassigned YES NVRAM administratively down down
FastEthernet0/1 unassigned YES NVRAM administratively down down
Serial0/1 unassigned YES NVRAM administratively down down
FastEthernet1/0 unassigned YES NVRAM administratively down down
FastEthernet2/0 unassigned YES NVRAM administratively down down
Loopback1 10.10.10.1 YES NVRAM up up
Loopback2 10.10.10.2 YES NVRAM up up
- Thực hiện mở và đọc tất cả các dòng trong file, in ra màn hình:
Code:
f = open("DATA.txt","r") # r - read đọc, nếu file CÓ TỒN TẠI thì sẽ mở ra và đọc, nếu KHÔNG có sẽ lỗi
print(f.read()) # đọc hết file
f.close() # đóng để giải phóng bộ nhớ
Noted:Với code trên nếu không tìm thấy file sẽ chương trình sẽ bị lỗi
và không thực hiện các lệnh bên dưới.
Kết quả:
C:\python>python Demo.pyInterface IP-Address OK? Method Status ProtocolFastEthernet0/0 192.168.122.11 YES NVRAM up upSerial0/0 unassigned YES NVRAM administratively down downFastEthernet0/1 unassigned YES NVRAM administratively down downSerial0/1 unassigned YES NVRAM administratively down downFastEthernet1/0 unassigned YES NVRAM administratively down downFastEthernet2/0 unassigned YES NVRAM administratively down downLoopback1 10.10.10.1 YES NVRAM up upLoopback2 10.10.10.2 YES NVRAM up upC:\python>
- Hoặc đọc từng dòng từng dòng một trong file
Code:
f = open("DATA.txt","r") # r - read đọc, nếu file CÓ TỒN TẠI thì sẽ mở ra và đọc, nếu KHÔNG có sẽ lỗi
print(f.readline()) # đọc từng dòng
print(f.readline())
print(f.readline())
print(f.readline())
f.close() # đóng để giải phóng bộ nhớ
Kết quả:
C:\python>python Demo.pyInterface IP-Address OK? Method Status ProtocolFastEthernet0/0 192.168.122.11 YES NVRAM up upSerial0/0 unassigned YES NVRAM administratively down down
Kết quả trên cho thấy là nếu muốn đọc 4 dòng thì chúng ta phải dùng 4 dòng
print(f.readline()) (vì có dòng trên cùng là dòng trắng trên cùng nên chúng ta thấy chỉ 3
dòng) cứ mỗi lần đọc như vậy con
trỏ đọc/ghi seek() sẽ trượt,
nếu chúng chúng ta muốn truy xuất lấy lại dữ liệu từ đầu file có 2 cách:
- Cách 1: đóng file và mở lại- Cách 2: đưa con trỏ seek() về đầu file bằng lệnh f.seek(0)
- Hoặc đọc từng dòng và đọc cho đến khi hết file.
Code:
f = open("DATA.txt","r") # r - read đọc, nếu file CÓ TỒN TẠI thì sẽ mở ra và đọc, nếu KHÔNG có sẽ lỗi
for pl in f: # đọc cho đến hết file
#print(pl) # kết quả sẽ chèn thêm dòng trắng sau mỗi dòng kết quả
print(pl, end="") # xóa bỏ dòng trắng
f.close() # đóng để giải phóng bộ nhớ
Kết quả:
2. Mở và tự động đóng file sau khi đọc.
Code:
'''
Cách mở file tự động đóng
nó sẽ tự động đóng khi kết thúc khối lệnh trong with
'''
with open("DATA.txt","r") as f:
output = f.read()
print(output)
Kết quả:
C:\python>python Demo.pyInterface IP-Address OK? Method Status ProtocolFastEthernet0/0 192.168.122.11 YES NVRAM up upSerial0/0 unassigned YES NVRAM administratively down downFastEthernet0/1 unassigned YES NVRAM administratively down downSerial0/1 unassigned YES NVRAM administratively down downFastEthernet1/0 unassigned YES NVRAM administratively down downFastEthernet2/0 unassigned YES NVRAM administratively down downLoopback1 10.10.10.1 YES NVRAM up upLoopback2 10.10.10.2 YES NVRAM up upC:\python>
3. Tạo và ghi file - Write
- Nếu file GhiDATA.txt chưa có thì tạo file mới với tên GhiDATA.txt
- Nếu file đã có thì ghi đè nội dung lên file này
Code:
'''
"w" - Write
- Nếu file chưa có thì tạo file mới,
- Nếu file đã tồn tại rồi thì xóa và ghi đè lên nội dung cũ
'''
f = open("GhiDATA.txt","w")
f.write("Ghi lan thu 1\n")
f.write("Ghi lan thu 2\n")
f.write("...\n")
f.write("...\n")
f.write("Ghi lan thu n")
f.flush() # thực hiện ghi nội dung vào file
f.close() # nếu không có dòng "f.flush()", khi đóng file thì python thực hiện flush trước khi đóng.
'''
Đọc file mới ghi
'''
f = open("GhiDATA.txt","r")
print(f.read())
f.close()
Kết quả:
C:\python>python Demo.pyGhi lan thu 1Ghi lan thu 2......Ghi lan thu nC:\python>
4. Ghi thêm dữ liệu vào file - Append
- Nếu file GhiDATA.txt chưa có thì tạo file mới với tên GhiDATA.txt
- Nếu file đã có thì chèn/thêm nội dung cần ghi vào cuối file
Code:
'''
"a" - Append
- Nếu file chưa có thì tạo file mới,
- Nếu file đã tồn tại mở và GHI THÊM vào cuối nội dung của file
'''
f = open("GhiDATA.txt","a")
f.write("\n")
f.write("GHI THEM VAO FILE\n")
f.write("Tiep tuc GHI THEM \n")
f.flush() # thực hiện ghi nội dung vào file
f.close() # nếu không có dòng "f.flush()", khi đóng file thì python thực hiện flush trước khi đóng.
'''
Đọc file mới ghi
'''
f = open("GhiDATA.txt","r")
print(f.read())
f.close()
Kết quả:
C:\python>python Demo.pyGhi lan thu 1Ghi lan thu 2......Ghi lan thu nGHI THEM VAO FILETiep tuc GHI THEMC:\python>
Xong!
No comments:
Post a Comment