Python Forum
Empty variable when using print before readline
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Empty variable when using print before readline
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
Ahhh i see next invocation reads the next line.
Never crossed my mind :)
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial Readline() conversion to array bprosman 1 1,909 Apr-11-2023, 12:44 AM
Last Post: deanhystad
  Greek letters with .readline() and tkinter KinkgOfKeks 7 1,751 Mar-24-2023, 05:13 PM
Last Post: deanhystad
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,073 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Print variable without '' and spaces arnonim 1 726 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  Problem with print variable in print.cell (fpdf) muconi 0 662 Dec-25-2022, 02:24 PM
Last Post: muconi
  Remove a space between a string and variable in print sie 5 1,783 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,949 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print variable between " paulo79 5 1,697 Mar-11-2022, 05:16 PM
Last Post: deanhystad
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,523 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  readline inside and outside functions paul18fr 2 2,042 May-20-2021, 01:15 PM
Last Post: csr

Forum Jump:

User Panel Messages

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