Posts

Showing posts with the label Reading File in Python

Python Tutorial 18 --- "Writing And Appending To A File"

Python Tutorial 18 --- "Writing And Appending To A File" This PROGRAM or TUTORIAL is about "Writing And Appending To A File" DOC: We have already discussed how to open and read a file in Python, in the last tutorial. If you haven’t read the blog, please go and see that one first. Now we will discuss how to write or insert text to a file and also how we can simultaneously read and write a file. Now, as we know, there are different modes of opening a file, we will cover three of them in this tutorial. Note: f is an object for the file. It is not a method or a special character. You will notice me using f.write() or f.read() or f.close(), in further description or tutorial, but you can use character or word of your own choice instead. MODES PURPOSE “w” mode:   Here “w” stands for write. After opening or creating a file, a function, f.write() is used to insert text into the file. The text is written inside closed parenthesis surrounded by double quotations. There is a ...

Python Tutorial 17 --- "Open(), Read() & Readline() For Reading File"

 Python Tutorial 17 ---  "Open(), Read() & Readline() For Reading File" This PROGRAM or TUTORIAL is about "Open(), Read() & Readline() For Reading File" DOC: As we now have an idea of what files(text or binary) are and their access modes, we are now ready to dive into the discussion of file handling methods. When we want to read or write a file (say on our hard drive), we must first open the file. When we open a file, we are asking the operating system to find the file by name, making sure the file exists. How to open a file ? Python has a built-in open() function to open a file. The syntax of the function is: open ( "filename" , "mode" ) Copy To open a file, we must specify two things, Name of the file and its extension Access mode where we can specify in which mode file has to be opened, it could either be read (r), write (w) or append(a), etc. For more information regarding access modes, refer to the previous tutorial. For Example,  o...