This post shows how to write text to files using Python 3. It involves opening or creating a file, writing texts, and closing it. We used Python 3.7.0 for the example.
Opening a File for Writing
In Python, before we can write to or read from a file, we need to open it. If the file doesn’t exist, we can specify to create it or not, especially when writing to it. Consider the Python codes below. Also, we can append a file by writing additional text to it. Meaning the previous content would still be there.
Open File to Write In Python
To open a file to write text in Python, we use the open function. Generally, it accepts two arguments — first, the file name, including the path. Second, the mode Python uses dictates what subsequent operations are valid.
1 2 3 4 5 6 | outfile = open('always_new_contents.txt', 'wt') print('This is my quest', file = outfile, flush = True) print('to follow that star', file = outfile, flush = True) print('no matter how hopeless', file = outfile, flush = True) print('no matter how far', file = outfile, flush = True) outfile.close() |
In line 1, we open a file always_new_contents.txt in the current directory with wt mode. The second argument means to create the text file if it didn’t exist. Otherwise, use an existing text file with the same file name. Moreover, the mode makes Python override any content the text file had. Consider the following output.
1 2 3 4 | This is my quest to follow that star no matter how hopeless no matter how far |
If we rerun the codes, the text file will have the same content as before.
Open File to Append With Text
When our codes always override the content of a text file, we lose the content the file had. Therefore, sometimes it is better to append by text file content with new data. Let us say we have another file that has the following content.
1 2 3 4 5 | When the night has come And the land is dark And the moon is the only light we'll see No I won't be afraid, no I won't be afraid Just as long as you stand, stand by me |
We would want to keep the original content plus any new text we may have added. Consider the following codes. In line 1, we are using the same Python open function but passing a different second argument at.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | outfile = open('existing_file.txt', 'at') print('So darlin\', darlin\', stand by me, oh stand by me', file = outfile, flush = True) print('Oh stand by me, stand by me', file = outfile, flush = True) print('If the sky that we look upon', file = outfile, flush = True) print('Should tumble and fall', file = outfile, flush = True) print('Or the mountains should crumble to the sea', file = outfile, flush = True) print('I won\'t cry, I won\'t cry, no I won\'t shed a tear', file = outfile, flush = True) print('Just as long as you stand, stand by me', file = outfile, flush = True) print('And darlin\', darlin\', stand by me, oh stand by me', file = outfile, flush = True) print('Oh stand now by me, stand by me, stand by me-e, yeah', file = outfile, flush = True) print('And darlin\', darlin\', stand by me, oh stand by me', file = outfile, flush = True) print('Oh stand now by me, stand by me, stand by me-e, yeah', file = outfile, flush = True) print('Whenever you\'re in trouble won\'t you stand by me, oh now now stand by me', file = outfile, flush = True) print('Oh stand by me, stand by me', file = outfile, flush = True) outfile.close() |
When we run the codes, Python generates the following new content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | When the night has come And the land is dark And the moon is the only light we'll see No I won't be afraid, no I won't be afraid Just as long as you stand, stand by me So darlin', darlin', stand by me, oh stand by me Oh stand by me, stand by me If the sky that we look upon Should tumble and fall Or the mountains should crumble to the sea I won't cry, I won't cry, no I won't shed a tear Just as long as you stand, stand by me And darlin', darlin', stand by me, oh stand by me Oh stand now by me, stand by me, stand by me-e, yeah And darlin', darlin', stand by me, oh stand by me Oh stand now by me, stand by me, stand by me-e, yeah Whenever you're in trouble won't you stand by me, oh now now stand by me Oh stand by me, stand by me |
If we rerun the codes, Python adds the same set of texts creating duplicate lines.