Welcome back! Sorry for the dry spell, but I've been rather busy setting up some side projects! If you remember back to a few training sessions ago, we covered basic file I/O. This is a very important step in making our scripts applicable in a real life scenario, and today we're going to further these concepts.
We'll be covering three I/O modes today, so let's take a moment and lay them out.
- r+ mode: This means that the file will be open for both reading and writing, which are typically done separately.
- rb mode: This stands for read binary. This mode allow us to easily read binary files.
- wb mode: This stands for write binary and it allows us to easily write binary files.
Now that we've ran through a brief of the modes we'll be covering, let's get right to it and start with the r+ mode!
Firstly, since the r+ mode isn't an acronym, I've taken to calling it extended reading mode. I'll be referring to it as such for the remainder of this training session.
Well, if we're going to perform some file I/O, we'll need a file. Let's go ahead and use the commands touch and cat to create our file and add some text to it:
Now that we have a file, let's boot up our trusty python environment and do some I/O!
If you remember from the previous file I/O session, we can automate the opening and closing process by opening the file under an alias. This is done via the keyword with followed by a standard open function, and then proceeded by the keyword as and the alias name (commonly named file). Let's take a look at this syntax, we'll read our file and write a new line to it:
Now, let's dissect this a little more. We've used a new method, .readlines(). This method reads the file line by line and generates a list with each line as it's own element. We then call the standard .write() method to add a fourth line to the file.
Now, let's go ahead and print our new list:
But, we have to keep in mind that since python in read line-by-line, our file was read before we wrote to it. So the contents of our file do not match that of our list. Let's go ahead and cat our file to see the difference:
There we have it! We can use extended reading (r+) mode to read from and write to a file at the same time. Now, let's move on to the 'rb' and 'wb' modes!
As you may be able to tell by the above headline, 'rb' and 'wb' stands for read binary and write binary. This makes it easy to read from and write to binary files.
If we're going to read binary files, we'll need a file to read. We'll be compiling a simple C program to print "Hello, World!" to the terminal. Let's make our file now:
Now that we have our binary file, test, we can use our binary read/write modes. Let's go back to our python shell and open our binary file with 'rb' mode:
Now, when we execute this print statement, a large amount of gibberish will be printed to the screen. We can't read it, but our system knows what it means. Let's go ahead and execute this print statement to see what reading a binary file looks like:
The only discernible thing we can see is the text "Hello, World!", but everything else is just nonsense. This is the result of printing the contents of a binary file. Now that we have the contents of our binary file stored under a variable, let's make a new file and write this to it. If we attempt to open a file that doesn't exist in write mode, the file will be created in the place specified in the open function. Let's open a new file named test2 and write our binary output to it:
Now that we've written this binary code to a file, we can actually execute this file just like we could the original. Let's demonstrate this and execute our new file:
There we go. We were able to effectively copy our binary file and execute it. These modes aren't exactly used often, but they're good to know just in case you encounter them.
We've covered a lot here, so let's wrap this up shall we?
We covered quite a bit here, so I hope it all got across well. The extended reading mode can be useful for performing mass file I/O. Honestly, the binary modes won't be used that often, but if they are, we'll still know what's going on!
The next article will be very important to this series, so if you're following along you won't want to miss it. I'll see you all in the next article!
Thank you for reading!
Start your White-Hat Hacker journey with our Beginner's Course on Linux and Shell Scripting Fundamentals (including BASH, Pearl, and Python).