Python Forum
[Basic] The ‘Pythonic’ Python Tutorial!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Basic] The ‘Pythonic’ Python Tutorial!
#5
https://github.com/the-machine-preacher/...iles.ipynb

In Cell 8:

with open('Examples/how_many_lines.txt') as lines_doc:
    for line in lines_doc.readlines():
        print(line)
It should be:

with open('Examples/how_many_lines.txt') as lines_doc:
    for line in lines_doc:
        print(line)
The example from Cell 8 opens the file,
then it iterates over all lines with readlines().
The list object is created in memory and holds the complete content of the file.

The correction of it, don't use readlines().
Iterating over a file, yields line by line without loading the whole content into memory.
The first function could not process a file, which is bigger as your RAM + Swap.


Another cool trick is the use of itertools.islice.
If you want to print the first 10 lines:

from itertools import islice


def head(file, lines):
    with open(file) as fd:
        for line in islice(fd, 0, lines):
            print(line.strip())


head(r'C:\Windows\system.ini', 10)
Tail have to be implemented different.
from collections import deque                
                                             
                                             
def tail(file, lines):                       
    result = deque(maxlen=lines)             
    with open(file) as fd:                   
        for line in fd:                      
            result.append(line.strip())      
    for line in result:                      
        print(line)                          

                                    
tail(r'C:\Windows\system.ini', 10)           
This is not optimal, because it has to read the whole file content.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: [Basic] The ‘Pythonic’ Python Tutorial! - by DeaD_EyE - Feb-10-2020, 09:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Basic] Basic Python Tutorial (Playlist) coffeejunkie34 0 908 Sep-28-2023, 12:18 PM
Last Post: coffeejunkie34
  Is there a good tutorial on installing Python correctly on Windows? CynthiaMoore 4 2,183 Aug-31-2022, 12:47 PM
Last Post: snippsat
  Complete Python Tutorial jamessidis 2 5,308 Dec-04-2020, 07:48 PM
Last Post: khalifa
  [Basic] Python Tutorial for Beginners MK_CodingSpace 0 2,236 Dec-03-2020, 09:43 PM
Last Post: MK_CodingSpace
  Python Scrapy Tutorial Knight18 0 3,981 Oct-19-2020, 04:56 PM
Last Post: Knight18
  12 mini games python tutorial series ninedeadeyes 2 2,480 Feb-16-2020, 11:18 PM
Last Post: ninedeadeyes
  [Basic] Python Tutorial for Beginners in One Lesson (98 minutes) artur_m 0 4,061 Jan-23-2020, 09:15 AM
Last Post: artur_m
  Complete Python 3 Tutorial Series KodeBlog 0 5,207 Sep-06-2019, 10:00 AM
Last Post: KodeBlog
  Tutorial for Python, Flask > data into a form Ecniv 1 2,451 May-01-2019, 07:49 PM
Last Post: Larz60+
  Python tutorial on learning & creating your own LRU Cache :) ncorbuk 0 3,525 Jan-26-2019, 08:28 PM
Last Post: ncorbuk

Forum Jump:

User Panel Messages

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