Saturday, May 22, 2021

Reading from a file: various methods

Script

#!/usr/local/bin/python3.5

import os
import time

adelaide = open('/Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt', 'r')
print()
print("Filename: ", adelaide.name)
print("Mode of opening the file: ", adelaide.mode)
print()
print("----------------------------------------------------------------------------------")
adelaide.close()

with open('/Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt', 'r') as adelaide:
  adelaide_contents = adelaide.read()
  print(adelaide_contents)

print("----------------------------------------------------------------------------------")

with open('/Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt', 'r') as adelaide:
  adelaide_contents = adelaide.readline()
  print(adelaide_contents)
  print("\n\n----------------------------------------------------------------------------\n\n")
print("The first 300 characters in the file")
print("====================================")
adelaide_contents = adelaide.read(300)
print(adelaide_contents)
print()
print("The next set of 300 characters in the file")
print("==========================================")
adelaide_contents = adelaide.read(300)
print(adelaide_contents)
print("\n\n----------------------------------------------------------------------------\n\n")

with open('/Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt', 'r') as adelaide:
  for line_by_line in adelaide:
print(line_by_line, end='')

print()
print("----------------------------------------------------------------------------------")
with open('/Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt', 'r') as adelaide:
  size_to_read = 600
  adelaide_contents = adelaide.read(size_to_read)
  while len(adelaide_contents) > 0:
print()
print("Reading 600 characters at a time.......")
print("Pausing for 3 seconds........")
time.sleep(3)
print(adelaide_contents, end='')
adelaide_contents = adelaide.read(size_to_read)

print("----------------------------------------------------------------------------------")
print()

with open('/Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt', 'r') as adelaide:
  size_to_read = 250
  adelaide_contents = adelaide.read(size_to_read)
  print("Understanding how the \"TELL\" option works.....")
  while len(adelaide_contents) > 0:
  print("We are currently at the position number: ", adelaide.tell())
  time.sleep(2)
  adelaide_contents = adelaide.read(size_to_read)


Background

ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > cat Adelaide-2003-K12.txt
Border-Gavaskar Trophy - 2nd Test
Australia v India
India won by 4 wickets
Test no. 1673 | 2003/04 season
Played at Adelaide Oval
12,13,14,15,16 December 2003 (5-day match)
  Australia 1st innings R M B 4s 6s SR
JL Langer c Sehwag b Kumble 58 116 72 72
ML Hayden c †Patel b Pathan 12 22 15 20
RT Ponting c Dravid b Kumble 242 508 352 31
DR Martyn c Laxman b Nehra 30 50 39 6
SR Waugh* b Nehra 30 69 53 5 0
SM Katich c Sehwag b Agarkar 75 136 109 9
AC Gilchrist† c Sehwag b Agarkar 29 34 24 4
AJ Bichel c Chopra b Kumble 19 51 45 2
JN Gillespie not out 48 74 53 6 0
BA Williams b Kumble 0 2 3 0 0
SCG MacGill lbw b Kumble 0 1 1 0 0
Extras (lb 8, w 1, nb 4) 13
  Total (all out; 127 overs; 537 mins) 556 (4.37 runs per over)

  India 1st innings R M B 4s 6s SR
A Chopra c & b Bichel 27 52 44 3 0
V Sehwag c Hayden b Bichel 47 69 41 6
R Dravid c Bichel b Gillespie 233 594 446 23
SR Tendulkar c †Gilchrist b Bichel 1 15 6 0
SC Ganguly* run out (Williams/MacGill) 2 10 7
VVS Laxman c †Gilchrist b Bichel 148 356 282 18
PA Patel† c Ponting b Katich 31 65 52 6
AB Agarkar c MacGill b Katich 11 31 30 1
A Kumble lbw b MacGill 12 53 44 1 0
IK Pathan c & b MacGill 1 24 13 0 0
A Nehra not out 0 18 8 0 0 0.00
Extras (b 4, lb 2, w 2, nb 2) 10
  Total (all out; 161.5 overs) 523 (3.23 runs per over)

  Australia 2nd innings R M B 4s 6s SR
JL Langer lbw b Agarkar 10 10 8 1 0
ML Hayden c Sehwag b Nehra 17 56 32 0
RT Ponting c Chopra b Agarkar 0 19 17 0
DR Martyn c Dravid b Tendulkar 38 111 76 4
SR Waugh* c Dravid b Tendulkar 42 86 65 8
SM Katich c Nehra b Agarkar 31 86 58 1
AC Gilchrist† b Kumble 43 59 45 4 2
AJ Bichel b Agarkar 1 5 6 0 0
JN Gillespie c †Patel b Agarkar 3 21 19 0
BA Williams not out 4 18 9 0 0
SCG MacGill b Agarkar 1 6 4 0 0
Extras (b 2, lb 2, w 1, nb 1) 6
  Total (all out; 56.2 overs; 248 mins) 196 (3.47 runs per over)

  India 2nd innings (target: 230 runs) R M B 4s
A Chopra lbw b Gillespie 20 64 54 2 0
V Sehwag st †Gilchrist b MacGill 47 120 81 7
R Dravid not out 72 241 170 7 0
SR Tendulkar lbw b MacGill 37 79 59 5 0
SC Ganguly* c Katich b Bichel 12 41 22 1
VVS Laxman c Bichel b Katich 32 44 34 6
PA Patel† b Katich 3 15 16 0 0
AB Agarkar not out 0 2 0 0 0 -
Extras (b 3, lb 6, w 1) 10
  Total (6 wickets; 72.4 overs; 306 mins) 233 (3.20 runs per over)
Did not batA Kumble, IK Pathan, A Nehra
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >


Execution

Filename:  /Python-scripts/Memorable-Tests/Adelaide-2003-K12.txt
Mode of opening the file:  r

----------------------------------------------------------------------------------
Border-Gavaskar Trophy - 2nd Test
Australia v India
India won by 4 wickets
Test no. 1673 | 2003/04 season
Played at Adelaide Oval
12,13,14,15,16 December 2003 (5-day match)
  Australia 1st innings R M B 4s 6s SR
JL Langer c Sehwag b Kumble 58 116 72 72
ML Hayden c †Patel b Pathan 12 22 15 20
RT Ponting c Dravid b Kumble 242 508 352 31
DR Martyn c Laxman b Nehra 30 50 39 6
SR Waugh* b Nehra 30 69 53 5 0
SM Katich c Sehwag b Agarkar 75 136 109 9
AC Gilchrist† c Sehwag b Agarkar 29 34 24 4
AJ Bichel c Chopra b Kumble 19 51 45 2
JN Gillespie not out 48 74 53 6 0
BA Williams b Kumble 0 2 3 0 0
SCG MacGill lbw b Kumble 0 1 1 0 0
Extras (lb 8, w 1, nb 4) 13
  Total (all out; 127 overs; 537 mins) 556 (4.37 runs per over)

  India 1st innings R M B 4s 6s SR
A Chopra c & b Bichel 27 52 44 3 0
V Sehwag c Hayden b Bichel 47 69 41 6
R Dravid c Bichel b Gillespie 233 594 446 23
SR Tendulkar c †Gilchrist b Bichel 1 15 6 0
SC Ganguly* run out (Williams/MacGill) 2 10 7
VVS Laxman c †Gilchrist b Bichel 148 356 282 18
PA Patel† c Ponting b Katich 31 65 52 6
AB Agarkar c MacGill b Katich 11 31 30 1
A Kumble lbw b MacGill 12 53 44 1 0
IK Pathan c & b MacGill 1 24 13 0 0
A Nehra not out 0 18 8 0 0 0.00
Extras (b 4, lb 2, w 2, nb 2) 10
  Total (all out; 161.5 overs) 523 (3.23 runs per over)

  Australia 2nd innings R M B 4s 6s SR
JL Langer lbw b Agarkar 10 10 8 1 0
ML Hayden c Sehwag b Nehra 17 56 32 0
RT Ponting c Chopra b Agarkar 0 19 17 0
DR Martyn c Dravid b Tendulkar 38 111 76 4
SR Waugh* c Dravid b Tendulkar 42 86 65 8
SM Katich c Nehra b Agarkar 31 86 58 1
AC Gilchrist† b Kumble 43 59 45 4 2
AJ Bichel b Agarkar 1 5 6 0 0
JN Gillespie c †Patel b Agarkar 3 21 19 0
BA Williams not out 4 18 9 0 0
SCG MacGill b Agarkar 1 6 4 0 0
Extras (b 2, lb 2, w 1, nb 1) 6
  Total (all out; 56.2 overs; 248 mins) 196 (3.47 runs per over)

  India 2nd innings (target: 230 runs) R M B 4s
A Chopra lbw b Gillespie 20 64 54 2 0
V Sehwag st †Gilchrist b MacGill 47 120 81 7
R Dravid not out 72 241 170 7 0
SR Tendulkar lbw b MacGill 37 79 59 5 0
SC Ganguly* c Katich b Bichel 12 41 22 1
VVS Laxman c Bichel b Katich 32 44 34 6
PA Patel† b Katich 3 15 16 0 0
AB Agarkar not out 0 2 0 0 0 -
Extras (b 3, lb 6, w 1) 10
  Total (6 wickets; 72.4 overs; 306 mins) 233 (3.20 runs per over)
Did not batA Kumble, IK Pathan, A Nehra

----------------------------------------------------------------------------------
Border-Gavaskar Trophy - 2nd Test



----------------------------------------------------------------------------


The first 300 characters in the file
====================================
Australia v India
India won by 4 wickets
Test no. 1673 | 2003/04 season
Played at Adelaide Oval
12,13,14,15,16 December 2003 (5-day match)
  Australia 1st innings R M B 4s 6s SR
JL Langer c Sehwag b Kumble 58 116 72 72
ML Hayden c †Patel b Pathan 12 22 15 20
RT Ponting c Dravid b Kumble 242 508 3

The next set of 300 characters in the file
==========================================
52 31
DR Martyn c Laxman b Nehra 30 50 39 6
SR Waugh* b Nehra 30 69 53 5 0
SM Katich c Sehwag b Agarkar 75 136 109 9
AC Gilchrist† c Sehwag b Agarkar 29 34 24 4
AJ Bichel c Chopra b Kumble 19 51 45 2
JN Gillespie not out 48 74 53 6 0
BA Williams b Kumble 0 2 3 0 0
SCG MacGill lbw b Kumble


----------------------------------------------------------------------------


Border-Gavaskar Trophy - 2nd Test
Australia v India
India won by 4 wickets
Test no. 1673 | 2003/04 season
Played at Adelaide Oval
12,13,14,15,16 December 2003 (5-day match)
  Australia 1st innings R M B 4s 6s SR
JL Langer c Sehwag b Kumble 58 116 72 72
ML Hayden c †Patel b Pathan 12 22 15 20
RT Ponting c Dravid b Kumble 242 508 352 31
DR Martyn c Laxman b Nehra 30 50 39 6
SR Waugh* b Nehra 30 69 53 5 0
SM Katich c Sehwag b Agarkar 75 136 109 9
AC Gilchrist† c Sehwag b Agarkar 29 34 24 4
AJ Bichel c Chopra b Kumble 19 51 45 2
JN Gillespie not out 48 74 53 6 0
BA Williams b Kumble 0 2 3 0 0
SCG MacGill lbw b Kumble 0 1 1 0 0
Extras (lb 8, w 1, nb 4) 13
  Total (all out; 127 overs; 537 mins) 556 (4.37 runs per over)

  India 1st innings R M B 4s 6s SR
A Chopra c & b Bichel 27 52 44 3 0
V Sehwag c Hayden b Bichel 47 69 41 6
R Dravid c Bichel b Gillespie 233 594 446 23
SR Tendulkar c †Gilchrist b Bichel 1 15 6 0
SC Ganguly* run out (Williams/MacGill) 2 10 7
VVS Laxman c †Gilchrist b Bichel 148 356 282 18
PA Patel† c Ponting b Katich 31 65 52 6
AB Agarkar c MacGill b Katich 11 31 30 1
A Kumble lbw b MacGill 12 53 44 1 0
IK Pathan c & b MacGill 1 24 13 0 0
A Nehra not out 0 18 8 0 0 0.00
Extras (b 4, lb 2, w 2, nb 2) 10
  Total (all out; 161.5 overs) 523 (3.23 runs per over)

  Australia 2nd innings R M B 4s 6s SR
JL Langer lbw b Agarkar 10 10 8 1 0
ML Hayden c Sehwag b Nehra 17 56 32 0
RT Ponting c Chopra b Agarkar 0 19 17 0
DR Martyn c Dravid b Tendulkar 38 111 76 4
SR Waugh* c Dravid b Tendulkar 42 86 65 8
SM Katich c Nehra b Agarkar 31 86 58 1
AC Gilchrist† b Kumble 43 59 45 4 2
AJ Bichel b Agarkar 1 5 6 0 0
JN Gillespie c †Patel b Agarkar 3 21 19 0
BA Williams not out 4 18 9 0 0
SCG MacGill b Agarkar 1 6 4 0 0
Extras (b 2, lb 2, w 1, nb 1) 6
  Total (all out; 56.2 overs; 248 mins) 196 (3.47 runs per over)

  India 2nd innings (target: 230 runs) R M B 4s
A Chopra lbw b Gillespie 20 64 54 2 0
V Sehwag st †Gilchrist b MacGill 47 120 81 7
R Dravid not out 72 241 170 7 0
SR Tendulkar lbw b MacGill 37 79 59 5 0
SC Ganguly* c Katich b Bichel 12 41 22 1
VVS Laxman c Bichel b Katich 32 44 34 6
PA Patel† b Katich 3 15 16 0 0
AB Agarkar not out 0 2 0 0 0 -
Extras (b 3, lb 6, w 1) 10
  Total (6 wickets; 72.4 overs; 306 mins) 233 (3.20 runs per over)
Did not batA Kumble, IK Pathan, A Nehra

----------------------------------------------------------------------------------

Reading 600 characters at a time.......
Pausing for 3 seconds........
Border-Gavaskar Trophy - 2nd Test
Australia v India
India won by 4 wickets
Test no. 1673 | 2003/04 season
Played at Adelaide Oval
12,13,14,15,16 December 2003 (5-day match)
  Australia 1st innings R M B 4s 6s SR
JL Langer c Sehwag b Kumble 58 116 72 72
ML Hayden c †Patel b Pathan 12 22 15 20
RT Ponting c Dravid b Kumble 242 508 352 31
DR Martyn c Laxman b Nehra 30 50 39 6
SR Waugh* b Nehra 30 69 53 5 0
SM Katich c Sehwag b Agarkar 75 136 109 9
AC Gilchrist† c Sehwag b Agarkar 29 34 24 4
AJ Bichel c Chopra b Kumble 19 51 45 2
JN Gillespie not out 48 74 53 6 0
BA Williams b Kumble 0
Reading 600 characters at a time.......
Pausing for 3 seconds........
2 3 0 0
SCG MacGill lbw b Kumble 0 1 1 0 0
Extras (lb 8, w 1, nb 4) 13
  Total (all out; 127 overs; 537 mins) 556 (4.37 runs per over)

  India 1st innings R M B 4s 6s SR
A Chopra c & b Bichel 27 52 44 3 0
V Sehwag c Hayden b Bichel 47 69 41 6
R Dravid c Bichel b Gillespie 233 594 446 23
SR Tendulkar c †Gilchrist b Bichel 1 15 6 0
SC Ganguly* run out (Williams/MacGill) 2 10 7
VVS Laxman c †Gilchrist b Bichel 148 356 282 18
PA Patel† c Ponting b Katich 31 65 52 6
AB Agarkar c MacGill b Katich 11 31 30 1
A Kumble lbw b MacGill 12 53 44 1 0
IK Pathan c & b MacGill 1 24 13 0 0
A
Reading 600 characters at a time.......
Pausing for 3 seconds........
Nehra not out 0 18 8 0 0 0.00
Extras (b 4, lb 2, w 2, nb 2) 10
  Total (all out; 161.5 overs) 523 (3.23 runs per over)

  Australia 2nd innings R M B 4s 6s SR
JL Langer lbw b Agarkar 10 10 8 1 0
ML Hayden c Sehwag b Nehra 17 56 32 0
RT Ponting c Chopra b Agarkar 0 19 17 0
DR Martyn c Dravid b Tendulkar 38 111 76 4
SR Waugh* c Dravid b Tendulkar 42 86 65 8
SM Katich c Nehra b Agarkar 31 86 58 1
AC Gilchrist† b Kumble 43 59 45 4 2
AJ Bichel b Agarkar 1 5 6 0 0
JN Gillespie c †Patel b Agarkar 3 21 19 0
BA Williams not out 4 18 9 0 0
SCG MacGill b Agarkar 1 6 4 0 0
Extras (b 2,
Reading 600 characters at a time.......
Pausing for 3 seconds........
lb 2, w 1, nb 1) 6
  Total (all out; 56.2 overs; 248 mins) 196 (3.47 runs per over)

  India 2nd innings (target: 230 runs) R M B 4s
A Chopra lbw b Gillespie 20 64 54 2 0
V Sehwag st †Gilchrist b MacGill 47 120 81 7
R Dravid not out 72 241 170 7 0
SR Tendulkar lbw b MacGill 37 79 59 5 0
SC Ganguly* c Katich b Bichel 12 41 22 1
VVS Laxman c Bichel b Katich 32 44 34 6
PA Patel† b Katich 3 15 16 0 0
AB Agarkar not out 0 2 0 0 0 -
Extras (b 3, lb 6, w 1) 10
  Total (6 wickets; 72.4 overs; 306 mins) 233 (3.20 runs per over)
Did not batA Kumble, IK Pathan, A Nehra
----------------------------------------------------------------------------------

Understanding how the "TELL" option works.....
We are currently at the position number:  250
We are currently at the position number:  504
We are currently at the position number:  754
We are currently at the position number:  1006
We are currently at the position number:  1260
We are currently at the position number:  1510
We are currently at the position number:  1764
We are currently at the position number:  2016
We are currently at the position number:  2268
We are currently at the position number:  2403






No comments:

Post a Comment