Every time you enter and exit the with open... block, you're reopening the file. As the other answers mention, you're overwriting the file each time. In addition to switching to an append, it's probably a good idea to swap your with and for loops so you're only opening the file once for each set of writes:

with open("output.txt", "a") as f:
    for key in atts:
        f.write(key)
Answer from glibdud on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_file_write.asp
Python File Write
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... with open("demofile.txt", "a") as f: f.write("Now the file has more content!") #open and read the file after the appending: with open("demofile.txt") as f: print(f.read()) Run Example ยป
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ inputoutput.html
7. Input and Output โ€” Python 3.14.3 documentation
A whence value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. whence can be omitted and defaults to 0, using the beginning of the file as the reference point. >>> f = open('workfile', 'rb+') >>> f.write(b'0123456789abcdef') 16 >>> f.seek(5) # Go to the 6th byte in the file 5 >>> f.read(1) b'5' >>> f.seek(-3, 2) # Go to the 3rd byte before the end 13 >>> f.read(1) b'd'
Discussions

Writing to file using Python - Stack Overflow
I have a file called output.txt, which I want to write into from a few functions around the code, some of which are recursive. Problem is, every time I write I need to open the file again and again... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How does one read and write to files in python?
https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files More on reddit.com
๐ŸŒ r/learnpython
6
2
February 24, 2021
Writing a for loop to a excel file

You could use a comma separated value to store the table.

More on reddit.com
๐ŸŒ r/Python
6
1
August 24, 2017
[Python] How do you write a non string to a file?
Convert the time to a string before writing it to the file: >>> time = 2.4 >>> str(time) '2.4' More on reddit.com
๐ŸŒ r/learnprogramming
3
6
July 14, 2013
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ writing-to-file-in-python
Writing to file in Python - GeeksforGeeks
December 27, 2025 - Before writing to a file, letโ€™s first understand the different ways to create one. Creating a file is the first step before writing data. In Python you control creation behaviour with the mode passed to open() (or with pathlib helpers). Note: You can combine flags like "wb" (write + binary) or "a+" (append + read).
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ write-to-file-python
How to Write to File in Python | LearnPython.com
Discover how to write to a file in Python using the write() and writelines() methods and the pathlib and csv modules.
๐ŸŒ
Real Python
realpython.com โ€บ read-write-files-python
Reading and Writing Files in Python (Guide) โ€“ Real Python
September 23, 2022 - Note: Some of the above examples contain print('some text', end=''). The end='' is to prevent Python from adding an additional newline to the text that is being printed and only print what is being read from the file. Now letโ€™s dive into writing files. As with reading files, file objects have multiple methods that are useful for writing to a file: Hereโ€™s a quick example of using .write() and .writelines():
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_write_to_file.htm
Python - Write to File
Opening a file with write permission treats it as a new file. To add data to an existing file without erasing its contents, you should open the file in append mode ('a'). The following example demonstrates how to open a file in append mode and ...
Find elsewhere
๐ŸŒ
CodingNomads
codingnomads.com โ€บ python-write-to-file
Python: Write to File
Edit the desktop file counter script that you built in the previous section to write its output to a file. Run the script and confirm that you can read the output in your new file. Take a new screenshot or add another file to your desktop, then run the script again. ... In a future lesson, you'll learn how you can improve this. You can use Python to write data to a file to persist the information after your script has finished running.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ creating-and-writing-file-python
Write to a file in Python - Python Morsels
October 18, 2021 - To write to a file in Python, you can use the built-in open function, specifying a mode of w or wt and then use the write method on the file object.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ file-handling-in-python
File Handling in Python โ€“ How to Create, Read, and Write to a File
August 26, 2022 - But if you retry the code above ... an error notifying you that the file already exists. It'll look like the image below: "w" โ€“ Write: this command will create a new text file whether or not there is a file in the memory with ...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-write-to-file
Python Write to File: Working With Text, CSV, and JSON Files | DataCamp
January 21, 2026 - The open() function connects your Python code to a file on disk. You tell it two things: ... For writing, the most common mode is "w", which means write. If the file doesnโ€™t exist, Python creates it.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ file_write.htm
Python File write() Method
The Python File write() method writes a string to a file. When the method writes the string into this file, it is first written into the internal buffer; and once this buffer is full, the contents are then transferred to the current file.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python basics โ€บ python write text file
How to Write to Text File in Python
March 30, 2025 - To open a file and write UTF-8 characters to a file, you need to pass the encoding='utf-8' parameter to the open() function. The following example shows how to write UTF-8 characters to a text file:
๐ŸŒ
Pythonspot
pythonspot.com โ€บ write-file
python write to file - Python Tutorial
March 9, 2016 - Python supports writing files by default, no special modules are required. You can write a file using the .write() method with a parameter containing text data.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ writing-files-using-python
Writing Files using Python
September 21, 2022 - This first example is pretty similar to writing to files with the popular programming languages C and C++. The process is pretty straightforward. First, we open the file using the built-in open() function for writing, write a single line of text to the file using the write() method, and then ...
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-file.html
File Read Write
Instead of coding the file-writing in your program, there is an alternative in the terminal that handles simple cases easily. This feature works in Mac, Windows, and Linux. Many programs print output to standard output. When you run the program in the terminal, you see this printed out right there, like this run of a super.py program that prints out that today is just great. $ python3 super.py Everything today is just super Most excellent $ What if we wanted to write that text to an out.txt file?
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-read-file-open-write-delete-copy
Python open() Function Explained: How to Open, Read, and Write Files | DigitalOcean
June 25, 2025 - You can check if a file exists with .exists(), read its contents with .read_text(), or write to it with .write_text(). To find files matching a pattern, you can use the .glob() method. This consolidation of functionality makes pathlib an efficient and powerful tool for all file system tasks. You can check if a file exists in Python using several methods.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ reading-writing-text-files-python
Reading and Writing to text files in Python - GeeksforGeeks
This article focuses on opening, reading, writing, and appending text files in Python.
Published ย  January 12, 2026