Solution 1

def read_first_line(path):
    with open(path, 'r') as fp:
        return fp.readline()

First Line from File

Write a function that receives a path to a text file as parameter, and returns the first line of that file.

Example:

  read_first_line('test-file.txt')  # "this is the first line"

Test Cases

test read first line - Run Test

import tempfile

def test_read_first_line():
    fp = tempfile.NamedTemporaryFile(mode="w")
    fp.write('this is line 1\n')
    fp.write('this is line 2\n')
    fp.write('this is line 3\n')
    fp.flush()

    assert read_first_line(fp.name) == 'this is line 1\n'

    fp.close()
def read_first_line(path): pass