Python File Handling

Python File Handling Part 3

File handling is one of the important feature in programming languages. Python provides a way to handle(read, write & modify) both text files as well as binary files. This tutorial will cover peek(), write(), writelines(), flush() and close() functions in detail.

➠ There are various functions available in Python for File handling. Click the function to get more detail about that function.
Peek: The peek() function can be used to peek into file content without moving the position of the cursor in the file. This function is only available for files opened in binary mode.

Write: The write() function takes one string parameter which can be used to write data into the file. The write() function returns the number of characters that were written by function(last call) on to file. File can be written multiple times using write() function.

WriteLines: The writelines() function also takes one string(which should have line delimiter) parameter which can be used to write stream of data into the file. The writelines() function returns the number of characters that were written by function(last call) on to file. This works almost similar to the write() function.

Flush: The flush() function can be used to flush data from Python temporary memory to file without closing the file. This is helpful for many use cases(such as making file available as soon as it is processed). It is good practise not to store too much data in the Python memory.

Close: The close() function is used to buffer out data onto the file for newly created or modified file. It is good practice to close out files once users are done with operations(read/write) on file.

****Visit File Handling part 1 for more functions