Write a python function Write_String(fanme, *list) which take a list containing string elements and write them in a file myfile.txt until the string “finish” (not case sensitive) occur. The function then returns the contents written in file as a string. Before writing list elements in file, you also need to make a separate python function to check whether input list contains "Finish" or not. If not, then it should return error as shown in example.
myfile.txt:
Python
Java
C
C++
Ruby
.Net
Example-1
Example-2
Example-3
Input:
myfile.txt
["Python", "Java", "C", "C++", "Finish"]
Output:
PythonJavaCC++
Input:
myfile.txt
["Python", "Java", "C", "C++", "Finish",".Net", "Rubby"]
Output:
PythonJavaCC++Input:
myfile.txt
["Python", "Java", "C", "C++"]
Output:
Error: List does not contain Finish
'''
Write a python function Write_String(fanme, *list) which take a list containing string elements and
write them in a file myfile.txt until the string “finish” (not case sensitive) occur.
The function then returns the contents written in file as a string. Before writing list elements in file,
you also need to make a separate python function to check whether input list contains "Finish" or not.
If not, then it should return error as shown in example.
myfile.txt:
Python
Java
C
C++
Ruby
.Net
Example-1
Example-2
Example-3
Input:
myfile.txt
["Python", "Java", "C", "C++", "Finish"]
Output:
PythonJavaCC++
Input:
myfile.txt
["Python", "Java", "C", "C++", "Finish",".Net", "Rubby"]
Output:
PythonJavaCC++Input:
myfile.txt
["Python", "Java", "C", "C++"]
Output:
Error: List does not contain Finish
'''
def Write_String(fname, List):
s = ""
if((List[len(List)-1]).lower()!="finish"):
s = "Error: List does not contain Finish"
else:
for r in range(0,len(List)-1):
s = s + str(List[r])
outfile = open(fname,"w")
outfile.write(s)
outfile.close()
return(s)
InputStr = ["Python", "Java", "C", "C++", "Finish"]
s = Write_String("myfile.txt",InputStr)
print("Output: ",s)
Comments
Leave a comment