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 Practice Problems 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 »
🌐
GeeksforGeeks
geeksforgeeks.org › python › writing-to-file-in-python
Writing to file in Python - GeeksforGeeks
1 week ago - 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).
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
Two ways of saving output to a file. Which one is better ? and why?
What’s the proper way of saving an output to a text file ? Both codes give the same result. file1 = open('output_test1.txt', 'w') for x in range(9): print(x, file =file1) file1 = open('output_test2.txt', 'w') for x in range(9): file1.write(str(x) + '\n') More on discuss.python.org
🌐 discuss.python.org
14
0
April 30, 2023
python - Correct way to write line to file? - Stack Overflow
How do I write a line to a file in modern Python? I heard that this is deprecated: print >>f, "hi there" Also, does "\n" work on all platforms, or should I use "\r\n... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.4 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'
🌐
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():
🌐
W3Schools
w3schools.com › python › ref_file_write.asp
Python File write() Method
The write() method writes a specified text to the file.
Find elsewhere
🌐
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 ...
🌐
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 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:
🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › files › writingFiles.html
8.8. Writing files — Python for Everybody - Interactive
>>> line2 = 'the emblem of our land.\n' >>> fout.write(line2) 24 · When you are done writing, you have to close the file to make sure that the last bit of data is physically written to the disk so it will not be lost if the power goes off. ... We should close the files which we open for read as well, but we can be a little sloppy if we are only opening a few files since Python ...
🌐
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 ...
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Read, Write, and Create Files in Python (with and open()) | note.nkmk.me
May 7, 2023 - In Python, the open() function allows you to read a file as a string or list, and create, overwrite, or append a file. Read and write files with open() and withEncoding specification: encoding Encodin ...
🌐
Scaler
scaler.com › home › topics › python › writing to file in python
Writing to File in Python - Scaler Topics
November 16, 2023 - Even if the text file does not exist, we can use the w or the a access mode to create a text file and then write to it. There are two functions in Python that help us write into text files:
🌐
Simplilearn
simplilearn.com › home › resources › software development › python write to file: an ultimate guide to write a file in python
Python Write to File: An Ultimate Guide to Write a File in Python
September 16, 2022 - The Python write to file is a set of functions that allow us to fiddle around with two file types ✓ text file and ✓ binary file. Learn all about this function!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States