Python Tutorial for Beginners 38 - Create a Text File and Write in It Using Python

 


hey guys welcome to the next blog on Python tutorial for beginners in this blog I'm going to show you how you can create a text file in Python and write in it so let's get started so to start with we use a built-in function which is open and it takes two argument first is the name of the file which we want to create or read so let's say I want to just create a file which is demo dot txt in the same folder in which I am working in so you can see I am working in my file handling project and there is only one file there which is test dot P Y and this demo dot txt file will be created in the same folder now the second argument which we want to give here is the mode so there are several types of modes which we can use with this open function to work with the files so let me show you the list of some of these modes so you can see on the left-hand side I have listed the modes so you can use the R or R plus or W or W plus or a or B now R is used to read from the file and if you don't provide any mode to this function then the default mode is the read mode now if you want to write to a file you use this write mode with a keyword W so we are going to use this W keyword in order to write to the file so let's use this W keyword here and then this open function returns a file object so let's store this file object into a variable now generally we call this file object a file handler and using this file handler we can work with the file on which we are working on so this file handler will be used to work on this file which we want to create now in the next line I will just use this file handler and then I can call some methods here you can see there is a list of methods I can call on this file handler which is returned by this open function so to write into a file I can use the write function and here inside the write function you can provide any text whatever you want to write to the file so let's say I just want to write this line of string to my text file which I am creating so I can just paste this text here and then after writing to a file when you are finished working with the file you use once again this a fetch and then call a method called closed on it now what does this close function do so this close function is used to close the file and immediately free up any system resources used by your file handler which is F H and it's a good practice to use this whenever you are done with the handling of your file so always remember to use this close function after you are done with the handling of your file so this is a good practice but I would say you must call this function and that's it so this W mode if the file demo dot txt doesn't exist it's going to create the file and then it's going to write this text into the file so W mode is responsible for creating the file also and if the file already exists it will overwrite the previous string by the string which you are writing using this write function so let me just run this code and you will keep eye on this project because this file will be created inside this project so I'm going to right click and then run this test file and you will see as soon as I run this test while there is one more file created here which is demo dot txt and when I click on this file you can see the same text appears here which we have written using the write function now if you want to provide the line breaks you can provide them using backslash N and I'm going to once again run the code so remember when the file doesn't exist the W mode is going to create the file and if the file already exists which it does now then it's going to overwrite it so I'm going to run the program once again and see the content of this demo dot txt and you can see this string is split into two lines which is done by this backslash n so let me just split the screen so you will see the demo in the real time so on the right hand side I have my demo dot txt file and on the left hand side I have this code on which I am working on so let's write something else in this file so instead of writing this big line of text into the file what I want to do now is I want to use a for loop to print 10 lines into this demo dot txt file so I'm going to use for loop for that and I'm going to just write for I in range and let's say the range is 10 and then we will call our write function using this FH file handler and then what I want to write here is let's say this is line number modulus D and I will just replace this mod D with the line number and then I will just provide the line break after that and in the previous blog we have already seen that when you use this modulo symbol and then give some value here for example I then this mod D will be replaced by this I right so let's run the code and see what happens now so I'm going to run this code and now you will see that this code prints this is line number zero this is line number one because the range starts from zero so in order to print the line number from 1 what we can do here is we can use this parenthesis here let me just move this to the side so inside the parenthesis we can use I plus 1 so we can add 1 every time this value of I is printed so it will start from wanna so let's run the code once again and you can see now it prints this is line number 1 line number 2 3 4 up to 10 now you might have already observed that whenever I run the code this demo dot txt file is overwritten every time when I run this code so the older text is overwritten by the new text now in order to avoid the overwriting of the text what we can do is we can use a special mode which is a here and a stands for append and this append mode is used to append the text to the file so let's replace this W mode with the a mode now okay and you can see this is the previous content of the demo dot txt file and now when I run the code you will see the previous content is staying there and the new content is appended to the same file so a we use to append to the existing content of the file now let me go to the list of modes once again and you will see there is a special mode W plus also which means if you want to read and write at the same time you use this W plus mode and this mode is going to create a new file if the file doesn't exist otherwise a file already exists it's going to overwrite too file which already exists also you will see a special mode B here which is used for the binary files such as images or other binary files this R and R plus mode we will see in the next blog and I'm going to show you how to read from the file using these modes in Python in the next blog so let's go to our code once again and in the blog in which I have shown you how to use exception handling I told you we can use try with the finally keyword so try and we can enclose this for loop inside the try block and let's do this and then in the finally we can add the closing of this file and what does this finally do if you remember this finally block is always called in the case exception is thrown by this code or if exception is not thrown so this F H dot close is definitely will be called whenever we use this finally keyword in order to call the F H dot close function so you can either use this kind of code in order to work with your file handler and then use this close function with this file handler at the end of the handling of the file otherwise items provide the shorter form of writing the same type of notation and this notation is you use a keyword width and then you call open function and you can provide the same type of argument here for example demo dot txt and append and then you use as keyword and then you write the name of the file handler for example F H as you have done here and then you provide this colon and inside this width statement you work with your code for example we work with the file writing with this for loop so this whole code including the try and finally block is equivalent to this kind of code which is the shorter form of writing the same code so this width statement is going to always call this closed method with your file handler when you are done with the handling of your file so you don't need to explicitly call this try finally block in order to work with your file this is enough to work with the file so either use this notation or this notation both are equivalent to each other now the last thing which I want to show here is let me just use the width statement in order to work with this demo dot txt file is for example you don't want to create this demo dot txt file into the current project instead you want to create this file into some folder on your computer so let's say I want to create this file inside this folder which is inside my C directory so I can just copy this whole part from here and then I can just paste this part with the backslash here and the important thing to note here is on Windows you always need to use the double backslashes in order to provide the file path okay so use these double backslashes to provide the file path so see inside the C directory I have this files folder and inside the file folder right now you can see this is empty and now when I run this code let me just change the mode to W which is for write mode and I'm going to run the code and as soon as I run the code this demo dot txt file is created here and you can also see the content of the file on the right side which is this is line 1 up to this is line number 10 so this is how you can create a text file and write into it using Python in the next  Blog I'm going to show you how to read from a file and work with the content of the file tuned and I'm going to see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News