如何从文本文件读取最后10个字节?

时间:2019-02-15 07:47:14

标签: python

Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

以上内容在我的文本文件中

如何从该文本文件读取最后10个字节?预期的输出是:

Good Luck!

1 个答案:

答案 0 :(得分:-1)

in_file.seek(-10, 2)  # where 2 denotes the reference point being the end of the file

然后:

in_file = open("demofile.txt", "rb") # opening for [r]eading as [b]inary
in_file.seek(-10, 2)
s = in_file.read(10)
print(s)

输出:

b'Good Luck!'