Python: ValueError: Mixing iteration and read methods would lose data

Once you iterate pdf_file by enumerate you cannot iterate it again except invoking pdb_file.seek(0) seek(0) changes the stream position to the beginning

Here’s my modification:

num = 1
for line in pdb_file:
    num += 1
    if "L01" in line:
       print num
       break 

pdb_file.seek(0)  # go back to the beginning and then it can be iterated again

last_host=int(num)
print(last_host-1)

for atom in range(0, last_host-1):
    data = pdb_file.readline()
    new_pdb_file.write(data) 

Leave a Comment