I am trying to create a program that saves input to a new file if it isn't there yet and it must always be on a new line. If the input is a whitespace it has to ignore it. Yet I cannot figure it out.
def saving(textr):
with open("abc.txt", "a+") as file_object:
file_object.seek(0)
data = file_object.read(100)
if textr == "" or textr.isspace():
return textr
else:
for line in file_object:
if textr in line:
break
elif len(data) > 0:
file_object.write("\n")
file_object.write(textr)
textr = input("Enter your text: ")
saving(textr)
and another way I have tried it:
textr = ""
def saving(textr):
textr = input("Enter your text: ")
with open("abc.txt", "a+") as file_object:
file_object.seek(0)
data = file_object.read(100)
if textr == "" or textr.isspace():
return textr
else:
for line in file_object:
if textr in line:
break
else:
if len(data) > 0:
file_object.write("\n")
file_object.write(textr)
print ("done")
saving(textr)