file-io demo

Open and read text file, read first line & last line, also use binary read. Only binary read can use reverse order seek, faster for large file processing. To test on larger text files.

Extends basic idea from Python Workout, by Reuven Lerner, c 2020 Manning, chapter 5
Start August 7, 2020 week,
Updates 9/3/2020 week

0. Check python version, active conda env

In [15]:
!python --version
Python 3.8.2
In [17]:
!conda env list  
# * is next to active conda env.  
# conda environments:
#
base                     /home/jyoon/conda3
dlpy                  *  /home/jyoon/conda3/envs/dlpy
fluentpy                 /home/jyoon/conda3/envs/fluentpy

1. Start practice

In [3]:
f = open('demofile.txt', 'rt')
blob = f.read()  # read all 
print(blob)
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

In [4]:
print(f.closed)  # check if f is closed at this point.
False
In [5]:
f = open('demofile.txt')  # Open again to go back to file start point.  
line = f.readline()  # read first line, no S
print(line)
Hello! Welcome to demofile.txt

In [6]:
f = open('demofile.txt')
lines = f.readlines()  # read all lines, iterable list. With S
print(lines[-1])  # print last line
f.close()
Good Luck!

2. Binary format reads 'rb'

In [7]:
# Try binary format reading.  
with open('demofile.txt', 'rb') as f:  # binary format
    first = f.readline()
    f.seek(-5, 2)  # Go to pointer 5th bytes from end of file 
    #last = f.read()  # does readline work?  May not read all of the last line.  
    last = f.readline()  # readline does work.  Still not all of the line.  
    print(first, last)
b'Hello! Welcome to demofile.txt\n' b'uck!\n'
In [8]:
# Seek from end of file needs a loop.  
with open('demofile.txt', 'rb') as f:  # binary format
    first = f.readline()
    
    f.seek(-2, 2)  # Go to pointer 2 bytes relative to end   
    while f.read(1) != b"\n":   # while read one byte is not equal to binary '\n'
        f.seek(-2, 1)   # go back 1 byte.  
    # after while loop, this is beginning of last line    
    last = f.read()  # read all bytes from here.   
    # last = f.readline() gives same output.  
    
    print(str(first)+"\n", str(last))
b'Hello! Welcome to demofile.txt\n'
 b'Good Luck!\n'

3. Good for short files, readlines() all at once, save, index [ ] into each line

In [9]:
# Good for short text files.  
with open('demofile.txt', 'rt') as f:  
    lines = f.readlines()
    first = lines[0]
    last = lines[-1]
    print(first, last)

print(f.closed)  # check file is cloased after "with" block end.  
Hello! Welcome to demofile.txt
 Good Luck!

True

4. seek(0) to go back to start, works with 'rt'

In [10]:
f2 = open('demofile.txt')  
lines = f2.readlines()
print("lines: ", lines)

f2.seek(0)  # Goto beginning of file
blob= f2.read()
print("blob: ", blob)

f2.seek(0)
one = f2.readlines(1)
print("one: ", one)

f2.seek(0)
two = f2.readlines()[1]  # Works!  prints 2nd item. nifty. 
print("two: ", two)
lines:  ['Hello! Welcome to demofile.txt\n', 'This file is for testing purposes.\n', 'Good Luck!\n']
blob:  Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

one:  ['Hello! Welcome to demofile.txt\n']
two:  This file is for testing purposes.

5. Only binary read 'rb' can use relative indexing

In [11]:
# seek can be used with text format?  
# Yes, but can't do relative location other than 0, current point. 
# If the file is opened in text mode (without ‘b’), only offsets returned by tell() are legal. 
# none of the relative indexing from back/current location works without 'b' binary format.  

with open('demofile.txt', 'rb') as f:  # need to be in binary format
    first = f.readline()
    print(first)
    
    f.seek(0, 0)  
    last = f.readlines()[2]
    print(last)
 
    #f.seek(-2, 2)  # not work, only 0 offset works with 2, 1 mode.  
    f.seek(0, 2)  # end of file work with text format file.  
    print(f.tell())
    f.seek(-11, 1)
    print(f.tell())
    last = f.read()
    print(last) 
b'Hello! Welcome to demofile.txt\n'
b'Good Luck!\n'
77
66
b'Good Luck!\n'
In [12]:
with open('demofile.txt') as f:  # text format again
    f.seek(0, 2)  # end of file  
    loc = f.tell(); print(loc)
    
    f.seek(0, 0)  # go back to beginning of tile 
    print(f.tell())
    first = f.readline()
    print(first, f.tell())
    
    last = f.readlines()[-1]
    print(last, f.tell())
    
    f.seek(31, 0)
    two = f.readlines()
    print(two, f.tell())
    
    f.seek(31, 0)
    second = f.readline()
    print(second, f.tell())
    
    third = f.readline()
    print(third)
77
0
Hello! Welcome to demofile.txt
 31
Good Luck!
 77
['This file is for testing purposes.\n', 'Good Luck!\n'] 77
This file is for testing purposes.
 66
Good Luck!

6. Doen't solve large file last line problem, but another way to move around 'rt' format.

In [14]:
with open('demofile.txt') as f:  # text format again
    
    # num_ln = len(f.readlines()); print(num_ln)
    
    num_ch_all = len(f.read())
    print(num_ch_all)
    
    f.seek(0)  # back to beginning
    num_ch_last = len(f.readlines()[-1]); 
    print(num_ch_last)
    
    offset = num_ch_all - num_ch_last  # offset from file beginning
    f.seek(offset, 0)
    print(f.tell())
    print(f.read())
77
11
66
Good Luck!

7. Read a slightly larger file, The Raven by Edgar Allen Poe

In [2]:
with open('TheRaven-Poe.txt') as raven:  # read as default text format 
    lines = raven.readlines()
    first = lines[0]
    last = lines[-1]
    print("first: ", first, "\n", "last: ", last)
    
first:  The Raven
 
 last:                                       Shall be lifted--nevermore!
In [10]:
print(raven.close())  # check it it closed
print(lines[0:10])
None
['The Raven\n', 'by\n', 'Edgar Allan Poe\n', '\n', '  Once upon a midnight dreary, while I pondered, weak and weary,\n', '  Over many a quaint and curious volume of forgotten lore--\n', '  While I nodded, nearly napping, suddenly there came a tapping,\n', '  As of some one gently rapping, rapping at my chamber door.\n', '  "\'Tis some visitor," I muttered, "tapping at my chamber door--\n', '                                     Only this and nothing more."\n']
In [11]:
print("Start of poem: ")
print(lines[4])
Start of poem: 
  Once upon a midnight dreary, while I pondered, weak and weary,

In [8]:
with open('TheRaven-Poe.txt') as raven:  # read again as text 
    blob = raven.read()  # Read whole thing  
    print(blob)
The Raven
by
Edgar Allan Poe

  Once upon a midnight dreary, while I pondered, weak and weary,
  Over many a quaint and curious volume of forgotten lore--
  While I nodded, nearly napping, suddenly there came a tapping,
  As of some one gently rapping, rapping at my chamber door.
  "'Tis some visitor," I muttered, "tapping at my chamber door--
                                     Only this and nothing more."

  Ah, distinctly I remember it was in the bleak December,
  And each separate dying ember wrought its ghost upon the floor.
  Eagerly I wished the morrow;--vainly I had sought to borrow
  From my books surcease of sorrow--sorrow for the lost Lenore--
  For the rare and radiant maiden whom the angels name Lenore--
                                     Nameless here for evermore.

  And the silken sad uncertain rustling of each purple curtain
  Thrilled me--filled me with fantastic terrors never felt before;
  So that now, to still the beating of my heart, I stood repeating
  "'Tis some visiter entreating entrance at my chamber door--
  Some late visiter entreating entrance at my chamber door;
                                     This it is and nothing more."

  Presently my soul grew stronger; hesitating then no longer,
  "Sir," said I, "or Madam, truly your forgiveness I implore;
  But the fact is I was napping, and so gently you came rapping,
  And so faintly you came tapping, tapping at my chamber door,
  That I scarce was sure I heard you"--here I opened wide the door--
                                     Darkness there and nothing more.

  Deep into that darkness peering, long I stood there wondering, fearing,
  Doubting, dreaming dreams no mortals ever dared to dream before;
  But the silence was unbroken, and the stillness gave no token,
  And the only word there spoken was the whispered word, "Lenore?"
  This I whispered, and an echo murmured back the word, "Lenore!"--
                                     Merely this and nothing more.

  Back into the chamber turning, all my soul within me burning,
  Soon again I heard a tapping something louder than before.
  "Surely," said I, "surely that is something at my window lattice;
  Let me see, then, what thereat is and this mystery explore--
  Let my heart be still a moment and this mystery explore;--
                                     'Tis the wind and nothing more.

  Open here I flung the shutter, when, with many a flirt and flutter,
  In there stepped a stately Raven of the saintly days of yore.
  Not the least obeisance made he; not a minute stopped or stayed he,
  But, with mien of lord or lady, perched above my chamber door--
  Perched upon a bust of Pallas just above my chamber door--
                                     Perched, and sat, and nothing more.

  Then the ebony bird beguiling my sad fancy into smiling,
  By the grave and stern decorum of the countenance it wore,
  "Though thy crest be shorn and shaven, thou," I said, "art sure no craven,
  Ghastly grim and ancient Raven wandering from the Nightly shore--
  Tell me what thy lordly name is on the Night's Plutonian shore!"
                                     Quoth the Raven, "Nevermore."

  Much I marvelled this ungainly fowl to hear discourse so plainly,
  Though its answer little meaning--little relevancy bore;
  For we cannot help agreeing that no living human being
  Ever yet was blessed with seeing bird above his chamber door--
  Bird or beast upon the sculptured bust above his chamber door,
                                     With such name as "Nevermore."

  But the Raven, sitting lonely on that placid bust, spoke only
  That one word, as if its soul in that one word he did outpour
  Nothing farther then he uttered; not a feather then he fluttered--
  Till I scarcely more than muttered: "Other friends have flown before--
  On the morrow _he_ will leave me, as my Hopes have flown before."
                                     Then the bird said "Nevermore."

  Startled at the stillness broken by reply so aptly spoken,
  "Doubtless," said I, "what it utters is its only stock and store,
  Caught from some unhappy master whom unmerciful Disaster
  Followed fast and followed faster till his songs one burden bore--
  Till the dirges of his Hope that melancholy burden bore
                                     Of 'Never--nevermore.'"

  But the Raven still beguiling all my sad soul into smiling,
  Straight I wheeled a cushioned seat in front of bird and bust and door;
  Then, upon the velvet sinking, I betook myself to linking
  Fancy unto fancy, thinking what this ominous bird of yore--
  What this grim, ungainly, ghastly, gaunt, and ominous bird of yore
                                     Meant in croaking "Nevermore."

  This I sat engaged in guessing, but no syllable expressing
  To the fowl whose fiery eyes now burned into my bosom's core;
  This and more I sat divining, with my head at ease reclining
  On the cushion's velvet lining that the lamp-light gloated o'er,
  But whose velvet violet lining with the lamp-light gloating o'er
                                     _She_ shall press, ah, nevermore!

  Then, methought, the air grew denser, perfumed from an unseen censer
  Swung by Seraphim whose foot-falls tinkled on the tufted floor.
  "Wretch," I cried, "thy God hath lent thee--by these angels he hath sent thee
  Respite--respite and nepenthe from thy memories of Lenore!
  Quaff, oh quaff this kind nepenthe and forget this lost Lenore!"
                                     Quoth the Raven, "Nevermore."

  "Prophet!" said I, "thing of evil!--prophet still, if bird or devil!--
  Whether Tempter sent, or whether tempest tossed thee here ashore,
  Desolate, yet all undaunted, on this desert land enchanted--
  On this home by Horror haunted--tell me truly, I implore--
  Is there--_is_ there balm in Gilead?--tell me--tell me, I implore!"
                                     Quoth the Raven, "Nevermore."

  "Prophet!" said I, "thing of evil!--prophet still, if bird or devil!
  By that Heaven that bends above us--by that God we both adore--
  Tell this soul with sorrow laden if, within the distant Aidenn,
  It shall clasp a sainted maiden whom the angels name Lenore--
  Clasp a rare and radiant maiden whom the angels name Lenore."
                                     Quoth the Raven, "Nevermore."

  "Be that our sign of parting, bird or fiend!" I shrieked, upstarting--
  "Get thee back into the tempest and the Night's Plutonian shore!
  Leave no black plume as a token of that lie thy soul has spoken!
  Leave my loneliness unbroken!--quit the bust above my door!
  Take thy beak from out my heart, and take thy form from off my door!"
                                     Quoth the Raven, "Nevermore."

  And the Raven, never flitting, still is sitting, still is sitting
  On the pallid bust of Pallas just above my chamber door;
  And his eyes have all the seeming of a demon's that is dreaming
  And the lamp-light o'er him streaming throws his shadows on the floor;
  And my soul from out that shadow that lies floating on the floor
                                     Shall be lifted--nevermore!
In [14]:
with open('TheRaven-Poe.txt', 'rb') as f:  # read as binary format 
    first = f.readline()
    print(first)
    
    for c in (1,2,3): # Move pointer past 3 lines
        start = f.readline()
    start = f.readline()  # Next line is start of poem  
    print(start)
    
    f.seek(-2, 2)  # Go to pointer 2 bytes relative to end   
    while f.read(1) != b"\n":   # while read one byte is not equal to binary '\n'
        f.seek(-2, 1)   # go back 1 byte.  
    # after while loop, this is beginning of last line    
    last = f.read()  # read all bytes from here.   
    # last = f.readline() gives same output.  
    print(last)
b'The Raven\r\n'
b'  Once upon a midnight dreary, while I pondered, weak and weary,\r\n'
b'                                     Shall be lifted--nevermore!'

8. Next, to test speed using 150 - 200 MB text file from Google Ngram dataset.

In [ ]:
 
In [ ]:
 
In [ ]:
 

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by OVHcloud

nbviewer GitHub repository.

nbviewer version: cf693bd

nbconvert version: 5.6.1

Rendered (Mon, 30 Nov 2020 14:26:13 UTC)