This must be in Python! Write a function that accepts a filename as input argume
ID: 3703897 • Letter: T
Question
This must be in Python!
Write a function that accepts a filename as input argument and reads the file and saves each line of the file as an element in a list (without the new line (" ")character) and returns the list. Ask the user :
Each line of the file has comma separated values: For example
Then the function should return a list such as :
Should catch the exception if file not existed. Let the system decide to display the error message.
O_exc.py 1 def list fr om_file(file_name): # Your code goes here # 4 5 6 7 def main): file_name - input("Enter the input file name: ) # Your code goes here # 13 10 12 13 14 mainExplanation / Answer
def list_from_file(file_name): lst = [] try: with open(file_name, 'r') as f: for line in f: lst.append(line.strip()) except: lst = [] return lst def main(): file_name = input('Enter the input file name: ') print(list_from_file(file_name)) main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.