Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีตั้งค่าตำแหน่งอ่าน/เขียนในไฟล์ด้วย Python

ใช้ seek(offset[, whence]) ตั้งตำแหน่ง whence: 0=absolute, 1=relative current, 2=relative end

ตัวอย่าง: ย้ายไปก่อน 'l' ตัวแรกใน "Hello\nworld"

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

ตำแหน่ง 2 คือหน้าตัว 'l' ตัวแรกของ "Hello"