Skip to content

Python write to file: Python file handling

Python write to file: introduction to file handling

Before I show you Python write to file: Python file handling, I assume you know how to use variables, how to write functions, and you can tell what difference between different data types in Python, so you should know these things, and if you’re not yet confident about those basic concepts nothing to worry about.

They are the fundamental blocks of the language so you need that for every program that you’ll write. So in this article, you’ll learn how to handle files with Python. So far we worked only with numbers, and text, and you know native objects in Python. In this article, we will interact with files.

So consider Python as a normal program such as Notepad editor or even Atom. Let’s say Notepad editor, so notepad can open text files and it can also write content in them and Python can do the same except you need to use code in the case of Python, so you need to write Python code to open a text file with Python for instance or other kinds of files and you use Python code to grab the text of the text file and you also do the same if you want to add text to that file so we read and write and the process is very easy. You just need to practice it a little bit. so you need to practice the commands that do this file handling. And I’ll be showing you all the necessary commands throughout the next paragraphs. These are built-in functions, so no need for third party libraries.

Check out my Python Beginners’ Course on Udemy

Python write to file: Opening and Reading a File

First, let’s learn how to open a file with Python and how to read the file content so that you can manipulate that content there in Python later on. For instance, you may need some data, you may need to read some data out of a file. Let’s say that would be a file where you have weather observation data and you want to open that with Python and then you want to send an email with python to let’s say your email address automatically, so python needs to read the content of that file as a string and then it sends that string as an email to your email address.

That’s just a random example, If you’re an experienced programmer you may find it easy to wrap your mind around how to use file handling techniques so about real-world scenarios. If you’re not a programmer will be using file handling techniques in real-world examples, so in the application that will be building. Nothing to worry about.

So let’s look at the syntax then. Python can be used to handle both binary and text files. Text files are files with txt extension, and you can open such files with editors such as atom. And we also have binary files such as dll file in your computer system folder which you cannot read with an editor. Mostly you’ll find yourself working with text files, so here is a simple text file and what I’m going to do now is I’d like to read these three lies in Python and print them out on the Python shell, so printing is crucial when you want to test things out.

Python file handling
File:Protocole HTTP Relation Client

Let’s say we’re about to write a program as I said that sends an email but first of all you want to take things step by step so you want to make sure that you’re reading this file correctly with Python. And to make sure that you’re reading that correctly, you want to print that out. You want to print the output in the shell.

Need a Python developer for your projects? How to Hire a Great Python Developer Today.

To read the content of a file with Python you first need to open that file using the open method and then you want to read the file content using the read method.

file=open("Example.txt",'r')

First of all, you want to create a variable. Let’s say file and then you use the open method which is a built-in method and then you point to the file that you want to open and there is one more argument there you need to pass through open methods, so the first argument is the file input and then you need to pass the mode that you want to open that file and R means read. So the most important modes are R and W. R is read and W is write.

Python file handling : opening a file in Python
Python file handling : opening a file in Python

That was successful using the code above. As long as you don’t get an error that is successful, so what is file? If you check what file is you’ll see that it’s a text file wrapper. Or you can call it a file object so what we did is we imported that file in Python form by creating a special object in Python and remember I told you that Python opens files as other programs do, so if you try now to delete example.txt you’ll get a message that “the action can’t be completed because the file is open in Python”

Python file handling: deleting a file opened in Python
Python file handling: deleting a file opened in Python

Once you have opened that file you may want to read it and to read the content of the file and it’s wise to save a content in a variable.

content = file.read()

I’m calling this variable content and then I’ll point to the file object which is saved in the file variable and then to the read method just like that and execute. Nothing happened so that was successful and now if you print content, you’ll get the text of the file which is actually a plain Python string.

Python file handling: Printing the content of a text file in Python
Python file handling: Printing the content of a text file in Python

Sometimes though you may want to save the lines in a list so instead of using Read you want to use Readlines

content = file.readlines()
print (content)

if you print content now you get an empty list which I’m sure it’s not what you were expecting, probably what you’re expecting was a list with six items so line 1 and line 2 and line 3 and so on…

 Python file handling: Printing the content of a text file into a Python  list (empty list)
Python file handling: Printing the content of a text file into a Python list (empty list)

The reason we didn’t get that is that you know when you apply the read method, before applying the read method there’s a concept called the pointer, so before applying the read method the pointer is before the first character of the text file and when you apply the read method what Python does is it reads everything that is from that pointer and down and so when the read method finishes, so after the read method, the pointer has gone at the last point of the file.

Python is trying to read everything that is from the pointer and down and there is nothing under the last point so that’s why we go this empty list. the solution to that is to apply a seek method there with an argument of 0.

file.seek(0)

So what that will do it will place the pointer back at the zero position of the text file, so if you execute that you get a message from Python which you don’t need, then if you apply again Readlines and then print content, this time you get a list of six elements.

 Python file handling: Printing the content of a text file into a Python  list
Python file handling: Printing the content of a text file into a Python list

. That’s what the seek method does. And I know at this point it’s hard to imagine why would you use the seek method, but you’ll understand how that comes into play, so it’s actually very useful in certain scenarios. It really gives you a lot of control over the files.

Back to the list and you see this \n characters there. backslash n in Python means a new line so, we have here line one. But then when I created this text file I did it like I wrote line one and then I press enter, so in other words enter is the backslash n and that is useful when you want to write content in a text file, so we’ll be using backslash later in the next paragraphs to write content in different separate lines in a text file and to get rid of those characters, to end up with a clean list with line 1, line 2, line 3 …etc there may be different walk arounds there but what I’m thinking of is a list comprehension, so our existing list is content and you can either create a new list, let’s say content1, a new variable to store the new list, the clean list or you can update your existing variables by passing content there and that would be equal to a new list so now you want to create this new list using a list comprehension, so that would be rstrip() which is a method that remove some characters from a string and in this case we would want to remove backslash n,

content1 = [i.rstrip("\n") for i in content]

so i there is a so temporary variable of this comprehension, an expression and you want to say for i in content. So we are applying the rstrip method to the i variable where i is the item of the content list so what this will do it will go through the first string and it will strip out the backslash n from that first string and then it will store it as first item of the new list and then it will go to the second line and it will do the same until the list is consumed so execute that t and now we have a clean list.

Using Python rstrip() method to remove characters from a list
Using Python rstrip() method to remove characters from a list

That’s what you should know about opening and reading text out of text files with Python, but there’s one last crucial thing you should know, you know we still have this file opened in Python. In this particular case, we’re opening that file in read mode, but if you were writing something in that file and then you close your terminal without closing the file, those changes would not be saved in the file, so you should always apply the close method to your file object

file.close()

Now you can go ahead and delete the file if you like, so that was successful. Again your variables are still there so once you read the content of the file and you store that in a variable, then you can reuse your variables as many times as you want.

Python write to file: Opening and Writing Text to a Text File

I’ll now go ahead and trigger a Python interactive session there and I’ll go ahead and create a file with Python again luckily the syntax is the same almost, so you use an open method and store the file that you’ll be creating in a variable. Let’s call this file, so let’s name it example.txt. And this time you don’t want to pass r, you want to pass w which stands for write. You execute that. And immediately you’ll get this example dot txt file and which for now it’s an empty file. So the open method what it does is it doesn’t only open an existing file. It also creates a file when that file doesn’t exist, so in this case if this example the text file was already in our working directory, then what Python would do, it would open that file and that file would be ready to write content inside it. When the file is not there, Python will create it, so now we can go ahead and say file.write. We don’t use read, we use write this time. And there you pass the string that you want to write in there, let’s say line 1. It executes and you get some return there which is not very important, but for your curiosity six means you wrote six characters to your file and now probably wondering and looking at this example the text file which is still example the text file which is still empty, so it doesn’t have the line 1 string written inside abd the reason to that is this example dot txt file is still open inside Python and every change that you’re making inside Python it only exists inside the Python program, so you haven’t yet saved that changes to your example.txt file, and that means when you open it with another program such as Atom you still get the old file. And if you’ll try to delete this file, you’ll not be able to do so because that is opened in Python, so to write the changes in there, you want to use the close method. Close and if you go to Atom, you’ll see a line one in the example.txt file and that’s how to write content inside a file. Something important that you should know is if you want to add a second line to this example.txt file, you probably would do, you’d open the file, so file open, example.txt in your write mode. And then you’d probably add line two in there and then probably close the file, but what you get is line 2 to only. The reason to that is the W method is not an append method, so for adding more lines to your existing text file, you’d want to use the append method which you’ll learn in the next paragraph. So write method what it does is it actually creates an empty file and then it allows you to write everything that you want inside that empty file. That said, it’s impossible to add multiple lines using the write method, but you will have to do that in one session, so that means you open, you create a text file and then say file write line 1 and then file write line 2 and then close the file and what you get is this text so you get both line one and two. But that’s probably not what you expected exactly. What you are expecting it’s probably a line 1 in a separate line and line 2 under that, so to do that they remember that we had this backslash n special character. That will allow you to create a new line just after line one and then when you write line 2 and then close the file, you’ll get a text file with two separate lines. However if you had to write a long list of lines there, let’s say you have 20 lines, ou want to write and execute in the write method 20 times wouldn’t be very efficient, so what you do is use a for loop to write all the lines using one single write method inside the loop so normally you’d have at least of any other object. You may have an excel file or CSV file with lines and let’s say you have three items in there. Line 1, line 2, line 3 and if you want to write these three lines in your example dot txt file you’d want to iterate. Let’s say for item in L which is a list file.write. Item here would be in the first iteration, item would be line 1 and then line 2, line 3, so you want to write item and maybe it’s a good idea to use backslash n, so after a line one you want to create a new line and then write a line 2 after that, so item plus backslash n. And yeah, that should do it. Nope that didn’t do it because Python cannot operate on a closed file. So this file variable is actually a closed file object now and you want to open that first because you know we closed it here and therefore you need to apply the open method again, so again file and open, you open that file. And then go ahead and say for item in L and we do this, execute. That was successful, but you still don’t see the changes in your txt file because you need to close the file. And yeah. Line 1, line 2, line 3. That worked perfectly, so if you get this error another time you know that you first need to open the file in Python, then you want, then you apply write or read methods to that file. So that’s about the write mode. In the next paragraph we’ll go ahead and append some more lines to this existing text file, so I’ll see you there.

Appending to a Text File

All right very quickly now let’s go ahead and add some lines in this example dot txt file and that process is called appending. Again you want to use open method, and again you may point to an existing file or if the file doesn’t exist it will be created and the argument you want to pass now is a. So that’s stands for appending. As you can observe now in contrast to the previous paragraph our text file was emptied but in this case we still have these three existing lines there. Again you use the write method to add anything in there. Line 4 or whatever, and normally the changes haven’t yet been reflected in the atom editor, so you want to close that. And now you see Line 4 added in there. Yeah, that was what I want to show you in this paragraph and I’ll see you later.

One Comment

  1. Hmm is anyone else encountering problems with the pictures on this
    blog loading? I’m trying to figure out if its a problem on my end or if
    it’s the blog. Any suggestions would be greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)