python读取文件内容并获得读取位置

2022-04-24 00:00:00 文件 读取 位置

下面的python代码演示了如何读取文件、获取读取指针的位置、从指定的位置开始读取文件

"""
皮蛋编程(https://www.pidancode.com)
创建日期:2022/4/24
功能描述:python读取文件内容并获得读取位置
"""

# Open a file
fo = open("/tmp/foo.txt", "r+")
str = fo.read(10)
print("Read String is : ", str)
# Check current position
position = fo.tell()
print("Current file position : ", position)
# Reposition pointer at the beginning once again
position = fo.seek(0, 0)
str = fo.read(10)
print("Again read String is : ", str)
# Close opend file
fo.close()

相关文章