Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách đặt vị trí đọc/ghi trong file Python?


Dùng seek(offset[, whence]). Thiết lập vị trí hiện tại file. whence mặc định 0 (absolute); 1 (relative current); 2 (relative end). Ví dụ file my_file nội dung Hello\nworld, muốn di chuyển trước chữ l đầu tiên:

f = open('my_file', 'r')
f.seek(2)
f.close()

Con trỏ đến trước chữ l đầu tiên.