How to Read Line by Line in File Python

Reading files line-by-line

Topic Series: Files

Trey Hunner smiling in a t-shirt against a yellow wall

Python Morsels

Scout as video

01:52

Let'southward talk about reading files line-by-line in Python.

Looping over file objects to read line-by-line

Here we're calling the read method on a file object (for a file called diary980.md):

                                      >>>                            filename              =              "diary980.doc"              >>>                            with              open up              (              filename              )              as              diary_file              :              ...                            contents              =              diary_file              .              read              ()              ...              >>>                            contents              'Python Log -- 24-hour interval 980\n\nToday I learned virtually metaclasses.\nMetaclasses are a course\'s grade.\nMeaning every class is an instance of a metaclass.\nThe default metaclass is "type".\due north\nClasses control features (like string representations) of all their instances.\nMetaclasses can control similar features for their classes.\n\nI uncertainty I\'ll ever need to make a metaclass, at least not for production lawmaking.\n'                      

When you call the read method on a file object, Python volition read the entire file into retention all at once. But that could exist a bad idea if you're working with a really big file.

There'southward another mutual way to process files in Python: y'all can loop over a file object to read it line-by-line:

                                      >>>                            filename              =              "diary980.md"              >>>                            with              open up              (              filename              )              equally              diary_file              :              ...                            n              =              1              ...                            for              line              in              diary_file              :              ...                            print              (              due north              ,              line              )              ...                            n              +=              1              ...                      

Hither, nosotros're press out a number (counting upward) in each line in our file:

                                      ane Python Log -- Day 980              ii              three Today I learned about metaclasses.              four Metaclasses are a form's grade.              v Significant every class is an example of a metaclass.              6 The default metaclass is "type".              vii              8 Classes control features (like cord representations) of all their instances.              9 Metaclasses can command similar features for their classes.              10              eleven I dubiousness I'll ever need to make a metaclass, at least not for product code.                      

Notice that as nosotros print, Python isn't just press out the line, but an extra blank line in betwixt each line in our file. By default, Python's print function prints a newline character (\north) afterwards whatever else that information technology prints (see the print function'due south terminate argument). But each of our lines also stop in a newline character, because newline characters are what dissever lines in a file:

                                      >>>                            line              "I doubt I'll ever need to make a metaclass, at least not for product lawmaking.\north"                      

Getting rid of the newline character when reading line-past-line

So we either need to suppress the newline character that the print function prints out or we demand to remove the newline characters from each line in our file every bit we impress them out:

                                      >>>                            filename              =              "diary980.doctor"              >>>                            with              open              (              filename              )              as              diary_file              :              ...                            northward              =              1              ...                            for              line              in              diary_file              :              ...                            impress              (              n              ,              line              .              rstrip              (              "              \n              "              ))              ...                            n              +=              1              ...              1 Python Log -- Day 980              two              3 Today I learned about metaclasses.              4 Metaclasses are a class'south class.              5 Meaning every class is an case of a metaclass.              6 The default metaclass is "type".              7              8 Classes control features (like string representations) of all their instances.              9 Metaclasses can control like features for their classes.              x              11 I uncertainty I'll ever need to brand a metaclass, at to the lowest degree not for production lawmaking.                      

We're using the string lstrip method here to "strip" newline characters from the left-hand side (the beginning) of each of our line strings just before print each line.

File objects are lazy iterables

File objects in Python are lazy iterables, which means we tin can treat them pretty much the same way as any other iterable.

So instead of manually counting upward, nosotros could laissez passer our file object to the built-in enumerate function. The enumerate part could then do the counting for us as we loop:

                                      >>>                            filename              =              "diary980.dr."              >>>                            with              open up              (              filename              )              as              diary_file              :              ...                            for              due north              ,              line              in              enumerate              (              diary_file              ,              start              =              1              ):              ...                            print              (              n              ,              line              .              rstrip              (              '              \n              '              ))                      

We've remove two lines of code but we get the aforementioned output every bit before:

                        1 Python Log -- 24-hour interval 980 two 3 Today I learned about metaclasses. 4 Metaclasses are a class's grade. 5 Pregnant every class is an instance of a metaclass. 6 The default metaclass is "blazon". 7 8 Classes control features (like cord representations) of all their instances. 9 Metaclasses tin can control similar features for their classes. x 11 I doubt I'll e'er demand to brand a metaclass, at to the lowest degree non for production lawmaking.                      

Summary

Files are lazy iterables, and equally nosotros loop over a file object, we'll become lines from that file.

When Python reads a file line-past-line, it doesn't store the whole file in memory all at once. Instead, it stores a small-scale buffer of upcoming lines in that file, and then information technology'south more memory-efficient.

That means looping over files line-by-line is especially important if y'all're working with really big files.

Topic Trail: Files

Reading from and writing to text files (and sometimes binary files) is an important skill for well-nigh Python programmers.

To track your progress on this Python Morsels topic trail, sign in or sign upward.

Write more Pythonic code

Need to fill-in gaps in your Python skills? I send regular emails designed to practice just that.

gordoninitime1973.blogspot.com

Source: https://www.pythonmorsels.com/reading-files-line-line/

0 Response to "How to Read Line by Line in File Python"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel