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
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
This code is not working can anyone please help?
with open('file','rb') as a :
try:
while True:
print(a.read(10)
except:
pass
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