NumberCruncher
NumberCruncher

Reputation: 29

Reading a text file in Python, what's the difference in each of these code examples

MyData.txt is a tab delimited file. It has 3 lines that look like this.

Adam 124 145

Beth 121 206

Andy 049 294

In the code below, I understand why the second for loop works (where i'm looping through "data"). I'm trying to understand why the first for loop part doesn't work. What does it mean to loop though "myfile" and why won't it print any values.

myfile = open("MyData.txt", "r")
data = myfile.readlines()

# This part doesn't work
for line in myfile:
    print(line)

# This part does!
for line in data:
    print(line)

myfile.close()

Upvotes: 0

Views: 756

Answers (3)

Anton vBR
Anton vBR

Reputation: 18906

There are numerous ways to read the content of a file and this answer could be infinite. The file objects have built in methods you can use. See this for instance: https://docs.python.org/3.6/tutorial/inputoutput.html#methods-of-file-objects

read()

method that reads all the content to one string


readlines()

method that reads all lines to a list (you are now at the end of the file)


readline()

method that reads one line (you are now on next row)


for row in file: ...

For loop (iterator) over the file. This will read it line by line See: How to read large file, line by line in python for instance.


So why not one way? Simply because there are tons of ways to read and write the data. Depending on the situation you will most likely want to use a different approach.

My personal favourite for smaller files is using .read() like this:

data = '''\
Adam 124 145
Beth 121 206
Andy 049 294'''

# Let's recreate the file
with open('MyData.txt','w') as f:
    f.write(data)

# Open file with 'with' to ensure it is closed
# Read the whole content and split it with '\n' linbreak
# Use a list comprehension

with open('MyData.txt','r') as f:
    data = [i.split(' ') for i in f.read().split('\n')] # Change ' ' to '\t' for tab

data variable is now:

[['Adam', '124', '145'], ['Beth', '121', '206'], ['Andy', '049', '294']]

Upvotes: 1

antonyjr
antonyjr

Reputation: 61

Whenever you use readlines or iterate over a file pointer , the position will be changed!

When you call myfile.readlines() , the position is changed to the end and thus when you iterate , there is no output , use myfile.tell() to find the position of your file pointer!

fp = open("Data.txt" , "r")
data = fp.readlines()
pos = fp.tell()

print("Position of File Pointer:: {}".format(pos))

Solution

You need to open the file twice. you can also use with

Upvotes: 0

Elmar Peise
Elmar Peise

Reputation: 15413

With

myfile = open("MyData.txt", "r")

you open a file handle, and

data = myfile.readlines()

reads all of the files content, meaning until the end of the file is reached.

Since after this, you reached the end of the file, there is nothing left to read from myfile in your first for loop, while you can iterate through the already read data as often as you like.

Upvotes: 1

Related Questions