Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what is the problem of this code? reading file in python

This code is not working can anyone please help?

with open('file','rb') as a :
    try:
        while True:
            print(a.read(10)
    except:
        pass

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use this one to read the file part by part.

with open('file.txt', 'rb') as a: #
    i = 0
    while True:
        a.seek(i) # Going to the i index
        data = a.read(10) # Read the next part
        if not data: # break if data is empty else continue
            break
        print(data) # print data
        i += 10

This one read the whole file at once but prints it part by part

with open('file.TXT', 'rb') as a:

    data = a.read()  # Reading the whole data
    dataLength = len(data)
    i = 10  # Substring length
    while i < dataLength:

        if (i + 10) > dataLength:
            print(data[i:])
        else:
            print(data[i: i+10])
        i += 10

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading