Cấu trúc dữ liệu Set trong Python
1. Đặc điểm của Set trong Python Set là cấu trúc dữ liệu giúp lưu trữ...
Kết quả sẽ mở trong tab mới, giới hạn trong website này.
Truy cập nhanh
Tìm kiếm gần đây
1. Ghi (write) file trong Python Để ghi (write) dữ liệu vào một file có sẵn, chúng ta có thể mở file với hàm open() với 2 chế độ: Giả sử, chúng ta có file gochocit.txt nằm cùng thư mục với file code .py của Python với nội dung như sau: Hello all, welcome to […]
Để ghi (write) dữ liệu vào một file có sẵn, chúng ta có thể mở file với hàm open() với 2 chế độ:
Giả sử, chúng ta có file gochocit.txt nằm cùng thư mục với file code .py của Python với nội dung như sau:
Hello all, welcome to gochocit.com!
Mở file gochocit.txt và ghi thêm dữ liệu vào file với hàm write().
f = open("gochocit.txt", "a")
f.write("\nVery happy to see you visit gochocit.com.")
f.close()
#open and read the file after appending
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Hello all, welcome to gochocit.com!
Very happy to see you visit gochocit.com.
Mở file gochocit.txt và ghi đè dữ liệu vào file.
f = open("gochocit.txt", "w")
f.write("Very happy to see you visit gochocit.com.")
f.close()
#open and read the file after overwriting
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Very happy to see you visit gochocit.com.Ở chế độ “w“, dữ liệu mới sẽ ghi đè lên toàn bộ dữ liệu đã có của file. Có thể hiểu đơn giản là dữ liệu cũ trong file sẽ bị xóa sạch và chỉ có dữ liệu mới được ghi vào file.
Python cũng hỗ trợ hàm writelines() để ghi một danh sách các dòng (line) vào file.
lines = ['Hello all, welcome to gochocit.com!',
'Very happy to see you visit gochocit.com.',
'Hope it might be useful for you!']
f = open("gochocit.txt", "w")
f.writelines('\n'.join(lines))
f.close()
#open and read the file after overwriting
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Hello all, welcome to gochocit.com!
Very happy to see you visit gochocit.com.
Hope it might be useful for you!
Ngoài ra, để ghi từng dòng vào file chúng ta có thể sử dụng vòng lặp for:
lines = ['Hello all, welcome to gochocit.com!',
'Very happy to see you visit gochocit.com.',
'Hope it might be useful for you!']
f = open("gochocit.txt", "w")
for line in lines:
f.write(line)
f.write('\n')
f.close()
#open and read the file after overwriting
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Hello all, welcome to gochocit.com!
Very happy to see you visit gochocit.com.
Hope it might be useful for you!
Nếu một file chưa tồn tại, Python cho phép tạo file với hàm open() ở các chế độ:
Các câu lệnh có thể giúp tạo một file gochocit.txt:
f = open("gochocit.txt", "x")hoặc
f = open("gochocit.txt", "a")hoặc
f = open("gochocit.txt", "w")Khi không cần sử dụng một file, Python hỗ trợ hàm remove() trong module os để giúp xóa (delete) file.
import os
os.remove("gochocit.txt")
Chúng ta sẽ gặp lỗi nếu xóa một file không tồn tại. Vì thế, nên kiểm tra một file có tồn tại hay không trước khi xóa file đó.
import os
if os.path.exists("gochocit.txt"):
os.remove("gochocit.txt")
else:
print("The file does not exist")