How to Write to a File in Python



Python


In this article, we show how to write to a file in Python.

It's relatively easy in Python to write to files.

These include writing to plaintext files, such as text files (.txt), html files (.html or .htm), PHP files (.php), etc.

These do not include more complex files such as Word documents and PDFs.

In order for Python to write to those files, extra modules need to be in place.

In this article, we show how to write to simple plaintext files, such as text files.

So the code to write to a file is shown below.



So this is the process.

In order to write to a file in Python, first you must open the file. So, you must use the open() function to first open the file. Inside of this open() function, you specify the file that you want to open. If the file is not located in the current working directory, you need to specify the full path to the file. If the file is located in the current working directory, then you can just enter the file directly, without specifying the path.

So now this openfile variable contains the File object of the file that we want to open.

We now have the File object, which represents the file.

We, then, take this File object, openfile, and use the write() function to write something to the file.

Inside of this write function, we specify the mode that we want to use in terms of writing. Basically, for writing, there are two types of modes. There is the 'w' mode for write. And there is the 'a' mode for append.

The 'w' mode overwrites everything in a file. Therefore, if the file contains something, it will be erased and replaced by whatever you enter inside of the parameters of the write() function.

The 'a' mode simply appends whatever you specify in the write() function to whatever is in the file. Therefore, if content is already in the file, it will not overwrite that content, like the 'w' mode. Instead, it simply appends to the content already in the file.

In the code above, we use the 'w' mode. This will wipe out anything in the file and replace it with the string we enter into the write() function. In this case, we enter the string, "Learning Python is not that bad". So this is what we write into the file, file.txt.

Next, after this, we must close the File object, openfile. We do this with the close() function. Without closing the File object after writing to a file, the contents is not written. If you were open a file after simply using the write() function without closing the File object with the close() function, the contents will not be written over. You must close it for the write operation to be complete.

And then after this, you will see that the contents have written to the file you specified.

Again, this works with simple plaintext files, such as text files, PHP files, HTML files. It will not work with more complex files such as binary files, such as Microsoft Word documents or PDFs.

So, again, this demonstrates writing to a file with 'w' mode. We will now show how to append to a file.


Appending to a File

So, above, we showed how to write to a file with the 'w' mode. This rewrites everything in the file with the text that you entered into the write() function.

However, you can also use the write() function with 'a' mode in which you simply append to the file (instead of overwriting everything on it).

The code to do so is shown below.



So, everything is the same as the code before, except now we open the file in 'a' (append) mode.

This appends the string to whatever currently exists in the file.

If you ran this code with the other code from before, you would have in the file, "Learning Python is not that bad. It really isn't."

So, this is how you can write to a file with Python.


Related Resources



HTML Comment Box is loading comments...