with open('1.txt', 'a+') as f: print('Input is starting, press q to quit.') loop = True while loop: input_string = input('please input something') if input_string != 'q': f.write(input_string + '\n') else: loop = False