with open('text.txt', 'r') as f:
print(list(f)[-1][0])
file_obj = open('myfile.txt')
the_text = file_obj.read()
*_, last_line = text.rsplit('\n', maxsplit=1)
first_char = last_line[0]
file = open("script.txt", "r")
last = ""
for line in file:
last = line
print(last[0])
小型文件解决方案。这将打开文件,并通过行迭代,直到文件完全迭代为止。
os.seek()
模块。
import os
with open('filename.txt', 'rb') as f:
f.seek(-2, os.SEEK_END)
while f.read(1) != b'\n':
f.seek(-2, os.SEEK_CUR)
last_line = f.readline().decode()
last_line_char = last_line[0]
从here编码
#做到这一点:
file = open("thefileyouwantoopen.txt", "r")
alllines = file.readlines();
#check for the length of the list displaying all the lines in the file
ouch = len(file.readlines()) - 1
#make a variable equal to the last line by calling the last index in the list
lastline = file.readline()[ouch]
#print the first digit
print(lastline[:1])