# q7 - create function readFileFirstLast() to meet the conditions below
# - accept a single parameter: a string (filename)
# - add a meaningful docstring
# - open and read each line of the file
# ---NOTE: the file is automatically generated when this file
# is run and it is placed in the same directory as this
# file
# --- You should catch a FileNotFoundError exception and return
# None if the file doesn't exist.
# - strip the newline character from each line
# - concatenate the first line and last line into a string
# - return that string
#
# - RESTRICTIONS: None
#
# - param: string
# - return: string

Respuesta :

Answer:

See explaination

Explanation:

def readFileFirstLast(filename):

# doc string

''' Function accept the filename and opens the fle

and reads all lines and strips new line character and

stores first and last in a string and return that string'''

#eception handle if file not found

try:

#opening the file

f = open(filename)

#reading the first line and striping the ne line

string = f.readline().strip()

#iterating until last line

for line in f:

pass

#concate the last line after strip the new line character to the string

string = string + " " + line.strip()

#return the string

return string

except:

#if file not found

return "File not found"

#taking the file name from user

filename = input("Enter a file name: ")

#printing the doc string in function

print("\ndoc_sting: \n"+ readFileFirstLast.__doc__+"\n")

#printing the returned string by calling the readFileFirstLast()

print("output string :")

print(readFileFirstLast(filename))