Welcome to Advance Python Script. This is the third course in the Python Scripting for DevOps Specialization. All right, this first module we're going to think about file input and output, right. We want to think about how we can persist data to files so that between executions of our scripts, we have the same data, okay? All right so, some learning objectives. By the time you're done with this module, I want you to be able to work with directories and files inside your python scripts. I want you to be able to develop computer programs that write text to files. I want you to build, develop computer programs that write binary data to files. I want you to be able to develop computer programs that read text from files. And lastly, I want you to be able to develop computer programs that read binary data from files, okay? All right, so in this first lesson, we're going to think of file handling. All right so, python has a function called open, it takes in two parameters the name of the file and the mode, the mode is optional, okay? And by default, if you don't specify a mode, the mode is the same as specifying the string r and r means read right. You're going to open a file for reading and if that file doesn't exist and air is going to be thrown, okay? a means append, it's going to open a file for appending, so it's going to put the pointer of the file at the very end. So when you write text, it will add to the bottom of the file and it's going to create the file if it doesn't exist with append. w is for write what it opens a file for writing and again it's going to create the file if it doesn't exist. x is create and it creates a file and returns an error if that file doesn't exist. And + can be added to any of the previous ones, and essentially it does reading and writing. So if I have w+, that means I'm going to open a file for writing and create the file that doesn't it does not exist, but I'm going to also be able to read from it an a+, same idea. We're going to open it for appending, we're going to put the pointer at the end, but I can also read from it. Okay, if I put the + in the r, it's going to open it for reading, but I can also write to it. Okay, but if the file doesn't exist, I'll get in there. Okay, so you can buy these together with our file modes again. File mode is optional if you don't specify, it's going to be the same as setting in r, all right. You can append a type of file to that mode string of either t for text, which is the default. So if you don't specify, it's going to assume you're going to interact with a file as if it's text and b as binary so, things like images or some specific format. So for example, a database writes data in binary format and binary data takes up less space often, okay, so here's some example code using the open. These are four different you wouldn't have all four of these are four different versions of open. So the first one we're going to open using the default mode of read and we're just opening the file demo file dot txt in the same directory as are executing program. The second one is going to specify, so the 1st and 2nd are actually the same. It's the second specifying the mode of read and text, which is the default. The third is going to use a path and on windows with the backslash for our path, we have to put double instances to escape. Write a single backslash means escape in a string, so we're escaping to the backslash. Essentially, if you will just have to remember whenever using a file in windows were using backslash for our directories, the limiters we put the double. So the third one is going to open a file called demofile dot text in the temp directory of a c. And it's going to open it for read and text. And the last one is going to open demo file dot text in the current directory for writing and it's a text also, okay? All right so, the file mode allows you to control how a file is accessed. You can add a tag to the file mode to specify if a file is to be open and text or binary and you always have to remember that backslashes and file names need to be escaped with a double backslash. All right, see you in the next lesson.