using For loop to read lines of texts from text files using python | applied electronics engineering


using For loop to read lines of texts from text files using python

By Applied Electronics - Wednesday, May 18, 2016 No Comments
In the last python programming tutorial for begineers, we described for loop in python and showed many examples of using it with regards to strings, tuples, list and dictionary. Also it was mentioned that for loop works not only with the in build objects such as strings, tuples, list and dictionary but also on other kinds of objects such as user defined objects. It also works on file objects which we are going to see here. The for loop can be used to read lines of text stored in a file such as txt file and other types of files.

We mentioned that for loop is used to access and retrieve information using the indices of iterable objects. Some few and simple examples are reshown here.

For loop is used for iteration with tuples, list, strings and any other iterable objects in Python.

using for loop to read text in python

Using for loop with tuples.

>>> for x in (1,2,3,4):
...     print(x*2)

Using for loop with list.

>>> for x in [1,2,3,4]:
...     print(x**2)

Using for loop with strings:

>>> for x in 'string':
...     print(x*2)

Now we show how to work with file object. Before that we show how traditionally we create or write file and read files.

Creating a file involves creating a file object or more specifically an instance of the class called open( ). Open takes arguments where the first argument is the name of the file to be created(or opened) and for creating a new file we have to provide the second argument w which stands for write.

Once the object f is created we write text using the write method. The text contains \n newline terminator to indicate the interpreter that the line is completed. To write next line we again use the write method. Finally we close the file which is optional at today's standard. Close ensures that the buffer is cleared.

>>> f = open('mytext.txt', 'w')
>>> f.write('Python programming language tutorial\n')
>>> f.write('text open/read tutorial\n')
>>> f.close()

Opening an existing file also involves instantiating an object from the open class. This time we don't need to specify the optional second argument but we can specify using r that it is a read mode. Then we can use the readline method to read the line of text in our text file. /n ensures here that readline should read upto the point where it encounters the \n.

>>> f = open('mytext.txt')
>>> f.readline()
'Python programming language tutorial\n'
>>> f.readline()
'text open/read tutorial\n'
>>> f.readline()
>>> f.readline()

To read the whole text in single command use the open( ).read( ) method as shown below.

>>> open('mytext.txt').read()
'Python programming language tutorial\ntext open/read tutorial\n'
>>> print(open('mytext.txt').read())
Python programming language tutorial
text open/read tutorial

Using For loop to read files in Python

We can use the For loop to iteratively read the texts in a file in Python as follows.

>>> for line in open('mytext.txt'):
...     print(line, end='')
Python programming language tutorial
text open/read tutorial

No Comment to " using For loop to read lines of texts from text files using python "