In: Computer Science
python:
Read the list of strings as separate lines from a text file whose file name is provided at run-time (Dr. Hanna's input file E29.txtPreview the document).
Write a function that takes a list of strings and outputs them, one per line, in a rectangular frame. For
example the list
[ "Hello", "World", "in", "a", "frame" ]
gets output as
*********
* Hello *
* World *
* in *
* a *
* frame *
*********
# reads the file and stores each word seprately in the list
def getList(filename):
# open the file in read mode
fptr = open(filename , 'r')
# store the text of the file in strng
strng = fptr.read()
arr = []
# read the text line by line
for line in strng.split('\n'):
# read word by word removing all the special characters
for word in line.split(' '):
arr.append(word)
return arr
def show_rectangular_frame(arr):
# store the length o the longest string in arr
max_len = 0
# get the length of longest string
for i in arr:
if len(i) > max_len:
max_len = len(i)
temp = '*' * ( max_len + 5 )
print(temp)
for i in arr:
print('*', i, ' *')
print(temp)
filename = input('Enter the name of the file : ')
arr = getList(filename)
show_rectangular_frame(arr)
Sample Output