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
with open("my file", "r") as f:
text = f.read()
a = text.split("\n")
if "Finish" in a:
a = a.replace("Finish", "", 1)
print(a)
else:
print("Error: List does not contain Finish")
Comments
Leave a comment