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

Question:

This code is not working can anyone please help?

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

Asked By: Frost Dream

||

Answers:

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

Answered By: codester_09
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.