Readlines in Python- Readlines ( ) is a function that is used in Python to read a particular file line by line in a single go.
- It is very useful in reducing the time complexity while reading a file using the brute force approach and using loops and different iterations.
- It is a one-line code and simple function, which much better than using many loops for just reading a file.
- In C programming, reading a file is quite a tough task, but it is very easy to implement due to this readlines ( ) function in Python.
- readlines ( ) function reads only one complete line from the given file; after reading, it returns the whole line containing all elements in the form of a list.
- If we want to open the file in normal read mode, readline ( ) function will return us the string.
- If we want to open the file in binary mode, then readline ( ) function will return you a binary object.
- It is also very beneficial to appends a newline ( " \n " ) at the end of the line.
- This readlines ( ) function is mostly suitable for small-sized files, containing fewer data to read the whole file within less time easily.
- It first reads the file's content to a memory in a single go and then separates them into various lines. With the help of the strip ( ) function, we can iterate over the whole list, which has been generated by the readline ( ) function, and by using the strip ( ) function, we can strip the newline ' \n ' character.
Python File conceptPython programming language has various inbuilt functions for writing, creating, and reading files. Two types of files are handled in Python, which is normal text files, and the second one is binary files mainly written in binary language that is 0's and 1's. - Text files: These files contain the data in the form of text, which is terminated with a special character called EOL ( end of a line ), the new line character ( ' \n ' ) in Python by default.
- Binary files: These types of files contain the data in the form of binary sequences, consisting of mainly 0's and 1's.
Certain basic steps to be performed in a file: Opening a file: Opening a file is done using the open ( ) function; while using this function, we must pass the file name and Access mode as an argument. Here the file access modes are as follows: - Read-only ( r ): It is used to read the data from the file. It is positioning the beginning of the file. If the file mentioned is not present, it will generate an input/output error. Read-only is the default mode of opening the file; while using this mode, we cannot edit the data of the file or cannot write it into the file.
- Write only ( ' w ' ): It is used to write the data into the file, positioning the beginning of the file. If the file mentioned is not present, then it will generate an input/output error. While using this mode, we cannot read the data from the file.
- Read and Write ( ' r+ ' ): This mode is used for writing and reading the data from the file. If we compare it with the previous mode, we can distinguish that we cannot write into the file in the read-only mode, but we can read and write into the file in this mode.
For example, if we have a file named hello.txt and we want to open it in write mode, then we can use it as: File1 = open ( " hello.txt " , " w " ) Closing a file: Close function is used to free up the memory space required by the file; this method is when there is no longer need for the file or if we want to close the entire file and want to open the file in a different mode. It is performed by using the close ( ) function, and inside this function, we are not required to pass any argument; this function can be accessed using the file name and supplied by the dot close function. For example, if we have a file named hello.txt and we want to open it in write mode, then we can use it as: File1 = open ( " hello.txt " , " w " ) File1.close ( )Writing in a file: As the name suggests, we can easily predict what we are required to write in a file in this method. There are two ways to write in a file: - Write ( ): In this function, we must pass a single argument, the string we want to insert into the file.
For example, if we have a file named hello.txt and we want to open it in write mode, then we can use it as: File1 = open ( " hello.txt " , " w " ) File1.write ( str ) Here, str is the string that we want to insert into the file. - Writelines ( ): In this function, we must pass an array or a list consisting of multiple strings. It is used to insert multiple various strings into the file at a single time.
For example, if we have a file named hello.txt and we want to open it in write mode, then we can use it as: File1 = open ( " hello.txt " , " w " ) File1.writelines( S ) for S = [ str1 , str2 , str3 ]
Reading from a file: Similarly, in this, we can predict that what we have to do in this; even in this tutorial, we are going to read among one of the reading methods in detail. In this method, we need to read the data from the particular file. There are three ways for reading the data from the file: - Read ( ): Returns the read bytes in the form of a string. Reads n bytes; if n is not specified, then reads the entire file.
- Readline ( ): Reads a line of the file and returns in the form of a string. For specified n, reads at most n bytes. readline ( ) function does not read more than one line at a time; even if n exceeds, it reads only one line. Readline ( ) function reads a line of the file and returns it in the string. It takes an integer value n as a parameter to read the number of characters read at a time. Readline ( ) method is very efficient in reading the data from a very large file because it fetches the data line by line and returns and prints on the screen. Readline ( ) returns the next line of the file, which contains a newline character in the end. Also, if the end of the file is reached, it will return an empty string.
- readlines ( ): Reads all the lines and returns them as a string element in a list. Readlines ( ) is used to read all the lines at a single go and then return them as a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then splits it into separate lines. Using the strip () function, we can iterate over the list and strip the newline ' \n ' character using the strip ( ) function.
Now let us understand the concept of reading a file in detail with the help of an example: Examples of reading a file in Python:Example 1: Reading a file using readlines ( ) function The output of the above example: Example 2: Reading a file using readline ( ) function The output of the above example: Example 3: Reading a file using simple for loop: In this method, we are not using the readline ( ) function and not even using the readlines ( ) function, as we have already seen the use of both of the functions in the above examples; in this method, we will use the for loop for printing the data of the file. We will iterate the objects of the file and read a file line by line, although we are using some python predefined built-in functions. Using these built-in Python functions, we can easily iterate over the file object implicitly using a for loop, in combination with using the iterable object. The output of the above example: Example 4: Reading a file using ' with ' statement: If we notice the above 3 examples, we can easily observe that every time whenever the file needs to be open, then it is required to be closed; if we do not close the file, then it will create several bugs in the program because many changes not done in the file or come into effect until we do not close the file. So, to overcome this problem, we will use the ' with ' statement, which is mainly used in exception handling in Python, to make the code clearer and much more readable. Here, in this example, you can easily observe that we are not using the file.close ( ) function, again and again, to prevent the file, using it can be automatically done with the statement. Hence it reduces lines of code and makes the program faster to run and implement more efficiently. The output of the above example:
|