Writing and Saving Files in Python¶
Author: Mohammad Sayem Chowdhury
Welcome! In this notebook, I'll share how I personally approach writing and saving files in Python. By the end, you'll see my favorite ways to write to a file and copy files, with my own commentary and tips.
What I'll Cover¶
- Writing to Files
- Copying Files
Estimated time: about 15 minutes, depending on your pace.
Writing to Files: My Approach¶
To save text to a file, I use Python's built-in open() function with write mode ('w'). Here's how I write a simple line to a file called Example2.txt.
# Writing a line to a file in my own style
with open('Example2.txt', 'w') as my_file:
my_file.write("This is line A")
Now, I'll read the file to check if the write worked as expected.
# Reading the file to verify the content
with open('Example2.txt', 'r') as check_file:
print(check_file.read())
This is line A
I often need to write multiple lines at once. Here's how I do it:
# Write lines to file
with open('Example2.txt', 'w') as writefile:
# The `write()` method lets me add new lines to a file, just like `readline()` reads them. I like to visualize each new line as a new row in my file.
writefile.write("This is line A\n")
writefile.write("This is line B\n")
The method .write() works similar to the method .readline(), except instead of reading a new line it writes a new line. The process is illustrated in the figure , the different colour coding of the grid represents a new line added to the file after each method call.
I always check the file after writing to make sure everything looks right.
# Double-checking the file content
with open('Example2.txt', 'r') as my_check_file:
print(my_check_file.read())
This is line A This is line B
If I want to add more lines without erasing the old ones, I use append mode ('a').
# Appending a new line to the file
with open('Example2.txt', 'a') as my_file:
my_file.write("This is line C\n")
Let's see if the new line was added.
# Checking the updated file
with open('Example2.txt', 'r') as my_check_file:
print(my_check_file.read())
This is line A This is line B This is line C
I often work with lists of lines. Here's how I write a whole list to a file:
# My sample list of lines
my_lines = ["This is line A\n", "This is line B\n", "This is line C\n"]
my_lines
['This is line A\n', 'This is line B\n', 'This is line C\n']
# Writing each line from the list to the file
with open('Example2.txt', 'w') as my_file:
for line in my_lines:
print(line)
my_file.write(line)
This is line A This is line B This is line C
I always verify my writes by reading the file back:
# Reading the file to confirm the write
with open('Example2.txt', 'r') as my_check_file:
print(my_check_file.read())
This is line A This is line B This is line C
Appending works the same way for lists. Here's how I add another line:
# Appending another line
with open('Example2.txt', 'a') as my_file:
my_file.write("This is line D\n")
Let's check the file again to see the new line.
# Final check after appending
with open('Example2.txt', 'r') as my_check_file:
print(my_check_file.read())
This is line A This is line B This is line C This is line D
Copying Files: My Method¶
Sometimes I need to make a copy of a file. Here's how I copy Example2.txt to Example3.txt:
# Copying one file to another
with open('Example2.txt', 'r') as source_file:
with open('Example3.txt', 'w') as dest_file:
for line in source_file:
dest_file.write(line)
I always check the new file to make sure the copy worked.
# Verify if the copy is successfully executed
# Checking the copied file
with open('Example3.txt', 'r') as my_check_file:
print(my_check_file.read())
This is line A This is line B This is line C This is line D
Beyond text files, I also like to experiment with saving data in formats like .csv or Excel. Python makes it easy to work with many file types.
You can always check your working directory to confirm your files are there and contain what you expect.
Copyright © 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the MIT License.
My Reflections¶
Writing and copying files in Python is something I do often, and it's always satisfying to see my data saved just the way I want. If you have your own tips or want to share your approach, I'd love to hear it!
Copyright © 2025 Mohammad Sayem Chowdhury. This notebook and its content are shared for personal learning and inspiration.
Final Thoughts¶
This notebook reflects my personal workflow for handling files in Python. I hope it helps you develop your own style and confidence with file operations.
— Mohammad Sayem Chowdhury