Posts

Showing posts with the label Reading Files

3 Advanced Methods to Read Files in Python

Image
Here are three advanced methods to read files in Python. Python files methods open, read and close are part of io.py. The methods are file_object.read(), file_object.readline() and file_object.readlines(). The read(x) method will read only the number of characters that mentioned in the read method. Again, if you give multiple read methods then it will read in sequence. What you'll learn here Example for read Example for readline Example for readlines Method-1: read method Here, file.txt is sample file with single row. file.txt abcdefghijk file_object.read(2) ==> you will get 'ab' file_object.read(4)  ===> you will get 'cdef' Here, the multiple read methods read the data in sequence. Method-2: readline method Here, file.txt is sample file with single row. file.txt abcdefghijk f ile_object.readline() ==> you will get ' abcdefghijk ' Here, it reads data line by line. Method-3: readlines method Here, file.txt is sample file with two rows rows. file.t