list_1 = ["Hello", "world", "it is me.", 2048] print(f"list_1: {list_1}") try: filename = 'output_1_out.txt' with open(filename, 'w') as f: print(f"list_1: {list_1}", file=f) # Note that the int 2048 is converted to '2048' by print() print("Hello", "world", "it is me.", 2048, file=f) print("Hello", "world", "it is me.", 2048, sep=" :-) ", file=f) print("Hello", "world", "it is me.", 2048, sep=" :-) ", end='\nbye bye\n', file=f) except FileExistsError: print(f"The file {filename} already exists.") except Exception as e: print(f"An error occurred: {e}") # Append. try: with open(filename, 'a') as f: f.write("\n\nThe following is written by write().") f.write("No newline symbol automatically added by write().\n") f.write(f"list_1: {list_1}") # no FileExistsError for append mode. except Exception as e: print(f"An error occurred: {e}")