Which order should you arrange the code segments to complete the function?

DRAG DROP
You are writing a function that works with files.
You need to ensure that the function returns None if the file does not exist. If the file does exist, the function must return the first line.
You write the following code:

In which order should you arrange the code segments to complete the function? To answer, move all code segments from the list of code segments to the answer area and arrange them in the correct order.
Select and Place:

microsoft-exams

2 thoughts on “Which order should you arrange the code segments to complete the function?

  1. I believe the answer is wrong. The first two lines should be switched.
    The line:

    if os.path.isfile(filename):

    should come first to check if the file exist.

    final code:

    import os

    def get_first_line(filename,mode):

    if os.path.isfile(filename):
    with open(filename,”r”) as file:
    return file.readline()
    else:
    return None

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.