10 examples of 'python read last line of file' in Python

Every line of 'python read last line of file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
82def _GetLine(self):
83 # type: () -> Optional[str]
84 line = self.f.readline()
85 if len(line) == 0:
86 return None
87
88 if not line.endswith('\n'):
89 self.last_line_hint = True
90
91 return line
42def read_line(filename):
43 """help function to read a single line from a file. returns none"""
44 try:
45 f = open(filename)
46 line = f.readline().strip()
47 f.close()
48 return line
49 except IOError:
50 return None
230def readline(self, limit=-1):
231 """
232 Public method to read one line from file.
233
234 @keyparam limit maximum bytes to read or all if limit=-1 (int)
235 @return decoded line read
236 """
237 txt = super(File, self).readline(limit)
238 if self.__encoding is None:
239 return txt
240 else:
241 return codecs.decode(txt, self.__encoding)
60def _read_file_lines(file_):
61 """Read lines of file
62 file_: either path to file or a file (like) object.
63 Return list of lines read or 'None' if file does not exist.
64 """
65 cont_str = _read_file(file_)
66 if cont_str is None:
67 return None
68 return [url_str.rstrip() for url_str in cont_str.splitlines()]
16def readline(self):
17 if self.stack:
18 self.line_number += 1
19 return self.stack.pop()
20 else:
21 line = self.f.readline()
22 if line != "":
23 self.line_number += 1
24 return line
44def first_line(path):
45 f = _open_file(path, READ)
46 line = f.readline()
47 f.close()
48 return line.rstrip('\n')
117def read(self): # Note: no size argument -- read until EOF only!
118 return ''.join(self.readlines())
11def read_input(file):
12 for line in file:
13 yield line.rstrip()
357def delete_last_line(file):
358 """
359 Delete the last line of the file handled by the "file" parameter
360 Adapted from https://stackoverflow.com/a/10289740
361 """
362
363 # Move the pointer (similar to a cursor in a text editor) to the end of the file
364 file.seek(0, os.SEEK_END)
365
366 # This code means the following code skips the very last character in the file -
367 # i.e. in the case the last line is null we delete the last line
368 # and the penultimate one
369 pos = file.tell() - 1
370
371 # Read each character in the file one at a time from the penultimate
372 # character going backwards, searching for a newline character
373 # If we find a new line, exit the search
374 while pos > 0 and file.read(1) != "\n":
375 pos -= 1
376 file.seek(pos, os.SEEK_SET)
377 pos += 1
378
379 # So long as we're not at the start of the file, delete all the characters ahead
380 # of this position
381 if pos > 0:
382 file.seek(pos, os.SEEK_SET)
383 file.truncate()
280def readlines(self):
281 """Read and return the list of all logical lines remaining in the
282 current file."""
283
284 lines = []
285 while True:
286 line = self.readline()
287 if line is None:
288 return lines
289 lines.append(line)

Related snippets