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
Note: If the file already exists, an error will be raised. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › writing-to-file-in-python
Writing to file in Python - GeeksforGeeks
2 weeks 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
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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › reading-and-writing-lists-to-a-file-in-python
Reading and Writing lists to a file in Python - GeeksforGeeks
July 23, 2025 - This function serializes your data structure into JSON format and writes it directly to a file. Here is an example: ... import json # Data to be written config_data = { 'name': 'John', 'role': 'developer', 'languages': ['Python', 'JavaScript'] } # Specifying the file name config_filename = 'config.json' # Writing the dictionary to a file in JSON format with open(config_filename, 'w') as config_file: json.dump(config_data, config_file) print(f"Data successfully written to {config_filename}")
Find elsewhere
🌐
YouTube
youtube.com › watch
How To Write To File In Python l Create a Text File and Write in It Using Python l Writing to File - YouTube
am going to show you How to Create a Text File and Write in It Using PythonCheap Webhosting get discount via my link https://panel.cinfu.com/aff.php?aff=80
Published   November 17, 2019
🌐
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():
🌐
Scaler
scaler.com › home › topics › python › writing to file in python
Writing to File in Python - Scaler Topics
November 16, 2023 - Achieving this in Python is easily done by utilizing the open() function with the mode parameter set to 'a' (append). ... Without precaution, attempting to write to the file directly would result in overwriting these lines.
🌐
University of Pittsburgh
sites.pitt.edu › ~naraehan › python3 › mbb13.html
Python 3 Notes: Writing Text to a File
Python 3 Notes [ HOME | LING 1330/2330 ] Tutorial 13: Writing Text to a File << Previous Tutorial Next Tutorial >> On this page: writing to a file, open('...', 'w'), .writelines(), .close(). Video Tutorial Python 3 Changes NONE! Python 2 vs. 3 Summary · Video Summary Writing a file within ...
🌐
Flexiple
flexiple.com › python › writing-file-python
Writing To File In Python - Flexiple - Flexiple
March 8, 2024 - The with statement simplifies file ... approach is both error-safe and concise. To write to a file, specify the file path and 'w' mode within the with statement....
🌐
GeeksforGeeks
geeksforgeeks.org › python › file-handling-python
File Handling in Python - GeeksforGeeks
In Python, writing to a file is done using the mode "w". This creates a new file if it doesn’t exist, or overwrites the existing file if it does. The write() method is used to add content.
Published   December 10, 2025
🌐
Code with C
codewithc.com › code with c › blog › python with open file write: writing to files in python
Python With Open File Write: Writing To Files In Python - Code With C
January 27, 2024 - Now, let’s talk about the main star of this show—open file write! This is a game-changer in Python file handling, as it allows us to not only create new files but also append data to existing ones. It’s like having the power to write the sequel to a legendary story or create a brand new tale altogether!
🌐
PYnative
pynative.com › home › python › file handling › writing list to a file in python
Python Writing List to a File [5 Ways] – PYnative
December 30, 2021 - Python writing a list to a text file. Use pickle module and json module to write and read a list into a binary and json file.
🌐
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'
🌐
Mimo
mimo.org › tutorials › python › how-to-write-to-a-file-in-python
How to Write to a File in Python
Write text files in Python with open() modes w/a, write() and writelines(), handle errors, and control where output is saved.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python write text file
How to Write to Text File in Python
March 30, 2025 - Second, write to the text file using the write() or writelines() method.
🌐
Medium
medium.com › @itylergarrett.tag › writing-to-text-files-in-python-2-7-a55cb7b8ddfd
How to write to a text file in Python 2.7 — Windows and Mac Tutorial. | by Tyler Garrett | Medium
March 7, 2019 - It generates a file, and adds our text to the file. If you’re on a mac, less work for you :), don’t say C:/… ... See how C:/ doesn’t work. Just use “/pyfun/output.txt” code pasted at bottom for easy copy paste :D · The python code above will use a few things you may have never seen before. Don’t sprint away, it’s all very easy to understand and extremely powerful to know if you’re a business user using python 2.7.
🌐
Tutorialspoint
tutorialspoint.com › python › python_write_to_file.htm
Python - Write to File
Writing to a file involves opening the file in a specific mode, writing data to it, and then closing the file to ensure that all data is saved and resources are released. Python provides a built-in function open() to handle file operations and
🌐
Cloudinary
cloudinary.com › home › how do i write to a file in python?
How Do I Write to a File in Python?
August 19, 2025 - Using a with block is the recommended way to write to files in Python.