Python Forum
Printing from a text file not working as I thought it would
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing from a text file not working as I thought it would
#1
Hi all! So my exercise is to read what's written in a text file and print it three times in three different ways. My solution is:

filename = 'learning_python.txt'

with open(filename) as file_object:
    contents = file_object.read()
    print(contents.strip())

with open(filename) as file_object:
    lines = file_object.readlines()

for line in lines:
    print(line.strip())

with open(filename) as file_object:
    for line in file_object:
        print(line.rstrip())
But I don't understand why this isn't working (it prints contents of the file only once instead of thrice):

filename = 'learning_python.txt'

with open(filename) as file_object:
    contents = file_object.read()
    print(contents.strip())
    for line in file_object:
        print(line.rstrip())
    lines = file_object.readlines()

for line in lines:
    print(line.strip())
I've read through the material again and it says nothing about this and I think it's important to understand this.

Thank you!
Reply
#2
After the first read() the file's pointer is positioned at the end of the file, so the "for line in file_object" starts at the end of the file, where there are no more records to be read. Take a look a seek() in the docs if you want to move the pointer back to the beginning of the file.
Reply
#3
(Jun-02-2018, 07:53 PM)woooee Wrote: After the first read() the file's pointer is positioned at the end of the file, so the "for line in file_object" starts at the end of the file, where there are no more records to be read. Take a look a seek() in the docs if you want to move the pointer back to the beginning of the file.

Oh, that's what happens! Thank you! I knew it would be useful to find out why this didn't work. Now I only need to find a way to reset the pointer position. Also thank you for your immediate response!
Reply
#4
I can't reproduce it. Here I am creating a file with some nonsenses in it and run Python. You can see that every time I get an output. Are you sure that this is the whole scenario and you don't miss something?

victor at Jerry in /m/s/A/M/Chill
↪ touch /tmp/test.txt
victor at Jerry in /m/s/A/M/Chill
↪ echo "onsones
  csicnsns
  snddddnsod
  jcsodncs" > /tmp/test.txt
victor at Jerry in /m/s/A/M/Chill
↪ cat /tmp/test.txt 
onsones
csicnsns
snddddnsod
jcsodncs
victor at Jerry in /m/s/A/M/Chill
↪ python
Python 3.6.4 (default, Jan  5 2018, 02:35:40) 
[GCC 7.2.1 20171224] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('/tmp/test.txt', 'r') as f:
...     content = f.read()
...     print(content)
... 
onsones
csicnsns
snddddnsod
jcsodncs

>>> with open('/tmp/test.txt', 'r') as f:
...     lines = f.readlines()
... 
>>> for line in lines:
...     print(line)
... 
onsones

csicnsns

snddddnsod

jcsodncs

>>> with open('/tmp/test.txt', 'r') as f:
...     for line in f:
...         print(line.rstrip())
... 
onsones
csicnsns
snddddnsod
jcsodncs
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Jun-02-2018, 07:58 PM)wavic Wrote: I can't reproduce it. Here I am creating a file with some nonsenses in it and run Python. You can see that every time I get an output. Are you sure that this is the whole scenario and you don't miss something?

The guy above answered correctly. Using seek(0) solved my problem. Thank you for your time :)
Reply
#6
(Jun-02-2018, 08:03 PM)PythonZenon Wrote: The guy above answered correctly. Using seek(0) solved my problem. Thank you for your time :)
The scenario @woooee talk about can occur,but it should occur in the code you posted.
Both second and third with open() will file object be overwritten in memory.
Output:
# test.txt hello world
filename = 'test.txt'
with open(filename) as file_object:
    print(id(file_object)) # memory location of file object
    contents = file_object.read()
    print(contents.strip())

with open(filename) as file_object:
    print(id(file_object)) # memory location of file object
    lines = file_object.readlines()
for line in lines:
    print(line.strip())
Output:
55480368 hello world 55480496 hello world
You can test yourself on a cloud Python version code,
to take your local setup out of the picture.
Reply
#7
(Jun-02-2018, 08:31 PM)snippsat Wrote: The scenario @woooee talk about can occur,but it should occur in the code you posted.
Both second and third with open() will file object be overwritten in memory.

I don't understand you and I don't understand what's the point you're trying to make unless you're trying to tell me that after each with open() file_object points to a different memory location.
Reply
#8
The point is that your first code should print what's in learning_python.txt 3 times.
(Jun-02-2018, 07:39 PM)PythonZenon Wrote: (it prints contents of the file only once instead of thrice):
Reply
#9
@snippsat
I think OP is asking why this code is not printing the text file 3 times:
filename = 'learning_python.txt'
 
with open(filename) as file_object:
    contents = file_object.read()
    print(contents.strip())
    for line in file_object:
        print(line.rstrip())
    lines = file_object.readlines()
 
for line in lines:
    print(line.strip())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(Jun-02-2018, 09:11 PM)buran Wrote: I think OP is asking why this code is not printing the text file 3 times:

You are correct :) I should have worded my question better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text conversion to lowercase is not working ineuw 3 480 Jan-16-2024, 02:42 AM
Last Post: ineuw
  Decryption not working if key has same symbol like text Paragoon2 0 320 Nov-11-2023, 09:32 PM
Last Post: Paragoon2
  while loop not working-I am using sublime text editor mma_python 4 1,149 Feb-05-2023, 06:26 PM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,134 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  ANSI not working for change of text colors BliepMonster 10 3,420 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,697 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,018 Feb-11-2022, 12:38 AM
Last Post: atomxkai
Question apk file not working on android polya001 0 1,161 Feb-06-2022, 11:58 PM
Last Post: polya001
  Subprocess.Popen() not working when reading file path from csv file herwin 13 15,125 May-07-2021, 03:26 PM
Last Post: herwin
  Printing x values from an csv file hobbyist 7 4,031 Mar-10-2021, 02:00 PM
Last Post: hobbyist

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020