Python Forum

Full Version: Empty variable when using print before readline
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Appreciate if someone can explain me why: Dodgy

with open('readme', 'r') as file_h:
    print 'file content:', file_h.readline().strip()
    content = file_h.readline().strip()
    print 'from variable:', content
python test.py
file content: Ek6uEmoKUAAAAAAACCTVU6bnsIAABn
from variable:

with open('readme', 'r') as file_h:
    # print 'file content:', file_h.readline().strip()
    content = file_h.readline().strip()
    print 'from variable:', content
python test.py
from variable: Ek6uEmoKUAAAAAAACCTVU6bnsIAABn
It works fine when I try it.
If you are running on windows, file explorer often is set to not show file extensions.
Why they chose to make this default, I'll never know.
open (from start menu) a cmd window.

navigate to the directory where the readme file is located and type dir
see if the readme file is actually named readme.txt
with open('readme', 'r') as file_h:
    # Read the only line to the end
    print 'file content:', file_h.readline().strip()

    # Will not nor re-read the line,so wlll try to read line 2
    content = file_h.readline().strip()
    print 'from variable:', content
So with this input:
Ek6uEmoKUAAAAAAACCTVU6bnsIAABn
123
This output:
Output:
file content: Ek6uEmoKUAAAAAAACCTVU6bnsIAABn from variable: 123
Ahhh i see next invocation reads the next line.
Never crossed my mind :)
Thank you!