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

Cách biết vị trí hiện tại trong file Python?


Lấy vị trí hiện tại của file object dùng phương thức tell. Ví dụ file my_file.txt nội dung Hello\nworld:

f = open('my_file.txt', 'r')
f.readline()
print(f.tell())
f.close()

Kết quả: 6 (con trỏ ở đầu từ 'world').