Python Read the First Half of File
Reading files line-by-line
Topic Series: Files
Watch as video
01:52
Allow'due south 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.md" >>> with open ( filename ) as diary_file : ... contents = diary_file . read () ... >>> contents 'Python Log -- Day 980\n\nToday I learned about metaclasses.\nMetaclasses are a course\'south class.\nMeaning every class is an instance of a metaclass.\nThe default metaclass is "blazon".\north\nClasses command features (like string representations) of all their instances.\nMetaclasses can control similar features for their classes.\n\nI doubt I\'ll ever need to make a metaclass, at least not for production code.\north'
When y'all call the read
method on a file object, Python will read the entire file into memory all at once. But that could be a bad idea if yous're working with a really big file.
In that location's another common fashion to process files in Python: y'all can loop over a file object to read it line-by-line:
>>> filename = "diary980.md" >>> with open ( filename ) as diary_file : ... n = 1 ... for line in diary_file : ... print ( n , line ) ... northward += 1 ...
Here, we're printing out a number (counting upwards) in each line in our file:
one Python Log -- Day 980 two three Today I learned about metaclasses. four Metaclasses are a class's class. v Meaning every class is an example of a metaclass. six The default metaclass is "blazon". 7 viii Classes command features (like string representations) of all their instances. 9 Metaclasses tin control similar features for their classes. 10 xi I dubiousness I'll ever need to make a metaclass, at least not for product code.
Notice that every bit we print, Python isn't just press out the line, but an actress blank line in between each line in our file. By default, Python'due south print
function prints a newline character (\north
) after whatever else that it prints (see the print
function's end
argument). Just each of our lines besides stop in a newline character, because newline characters are what separate lines in a file:
>>> line "I dubiousness I'll ever demand to brand a metaclass, at least not for production lawmaking.\n"
Getting rid of the newline graphic symbol when reading line-by-line
So nosotros either need to suppress the newline character that the print
role prints out or we need to remove the newline characters from each line in our file as we print them out:
>>> filename = "diary980.md" >>> with open ( filename ) as diary_file : ... n = 1 ... for line in diary_file : ... impress ( n , line . rstrip ( " \n " )) ... n += 1 ... 1 Python Log -- Day 980 two iii Today I learned most metaclasses. 4 Metaclasses are a class's grade. 5 Significant every form is an instance of a metaclass. 6 The default metaclass is "type". vii 8 Classes command features (like string representations) of all their instances. 9 Metaclasses can control similar features for their classes. 10 11 I doubt I'll ever need to make a metaclass, at least non for production code.
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 earlier impress each line.
File objects are lazy iterables
File objects in Python are lazy iterables, which means we can treat them pretty much the same way equally any other iterable.
So instead of manually counting upwards, we could pass our file object to the congenital-in enumerate
part. The enumerate
function could so do the counting for us every bit nosotros loop:
>>> filename = "diary980.md" >>> with open ( filename ) as diary_file : ... for due north , line in enumerate ( diary_file , get-go = 1 ): ... print ( n , line . rstrip ( ' \n ' ))
Nosotros've remove two lines of code but we get the same output as before:
one Python Log -- Day 980 two 3 Today I learned about metaclasses. 4 Metaclasses are a class'due south course. 5 Meaning every class is an example of a metaclass. 6 The default metaclass is "blazon". 7 8 Classes control features (similar string representations) of all their instances. 9 Metaclasses can control like features for their classes. 10 11 I doubt I'll ever need to make a metaclass, at least not for production code.
Summary
Files are lazy iterables, and every bit we loop over a file object, we'll go lines from that file.
When Python reads a file line-past-line, information technology doesn't shop the whole file in memory all at once. Instead, information technology stores a small buffer of upcoming lines in that file, so it's more retentiveness-efficient.
That means looping over files line-by-line is especially important if you'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 most Python programmers.
To track your progress on this Python Morsels topic trail, sign in or sign up.
✕
↑
Write more Pythonic code
Need to fill up-in gaps in your Python skills? I send regular emails designed to exercise just that.
Source: https://www.pythonmorsels.com/reading-files-line-line/
0 Response to "Python Read the First Half of File"
Post a Comment