File access and manipulation

We can read, write, and update files in Python. Python has got an open construct that can be used to provide file manipulation operations. When we open a file, there are various modes in which that file can be opened, as shown follows:

  • r: Read mode, this reads the file in text mode (Default).
  • rb: This reads the file in binary mode.
  • r+: This reads the file in both read and write mode.
  • rb: This opens the file for reading and writing in binary mode.
  • w: This opens the file in write mode only. It overwrites the existing file.
  • wb: This opens the file for writing in binary mode. It overwrites the existing file.
  • w+: This opens the file in both write and read mode. It overwrites the existing file.
  • wb+: This opens the file for both reading and writing in binary mode. It overwrites the existing file.
  • a: This opens the file in append mode and creates a file if it doesn't exist.
  • ab: This opens the file in append binary mode and creates a file if it doesn't exist.
  • a+: This opens the file in both append and read mode and creates a file if it doesn't exist.
  • ab+: This opens the file in append read binary mode and creates a file if it doesn't exist.

In the following code block, the first argument to the open method call is the path of the file to be opened. The second is the mode in which the file has to be opened, and the third is the optional buffering argument that specifies the file's desired buffer size: 0 means unbuffered, 1 means line-buffered, and any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means that the system default should be used. This is usually line-buffered for tty devices and fully buffered for other files. If omitted, the system default is used.

open("filepath","mode",buffer)

With buffering, instead of reading directly from the operating system representation of the raw file (which would have high latency), the file is instead read into a OS buffer and read from there from then on. The advantage of this is that if we have a file present on the shared network and our objective is to read the file every 10 ms. We can load it once in the buffer and then read it from there, instead of reading it from the network each time, which would be expensive.

Take a look at the following snippet from the File_access.py file to understand more:

The code snippet in the preceding screenshots from the File_access.py file explains how to use files in Python. The read() method of the File class takes the file path and if the whole path is not given, then the current working directory is assumed to be the starting path. The read() method invoked on the file instance reads the whole file into the program variable. read(20) will load 20 bytes from the file in the current file pointer position. This is very handy when we are dealing with large files.

The readlines() method returns a list, with each entry referring to each line of the file. The readline() method returns the current line from the file. The seek() method will take the file pointer to the position specified in the argument. Therefore, whenever we execute seek(0), the file pointer points towards the beginning of the file:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.157.45