Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python, printing a line from a file i need to open the file with a function ive

ID: 3723516 • Letter: P

Question

python, printing a line from a file

i need to open the file with a function ive made, i think ive got that, now i want it to print just the first line

In [1 def open_function (b) f-open(b, 'r for i in range (1): bbb - Notepad File Edit Format View Help testbline 1 line 2 line 3 first-line-f.readline() #### ???? is this not right??? rest-f.readlines() f.close In [2]: open function ("bbb.txt') print(first_line) Traceback (most recent call last) NameError python-input-2-8c6b9aaf22ad» in --> 1 open-function (bbb.txt) () 2 print(first_line) NameError: name 'bbb' is not defined In :

Explanation / Answer

def open_function(b): f = open(b, "r") # open a file. use open(b, "r") instead of open("b", "r") return f.readline() # returns first line # then you can open any file you want. Just make sure that file is available first_line = open_function('bbb.txt') # get and print first line print(first_line)