I recognize that this is because num is within the an initial for loop and last_host is dependend of num yet I have no idea exactly how to work about this.

#!/usr/bin/env python2import datetime together dtimport glob together globimport os as osimport shutil as shutilimport signal together signalimport subprocess as spimport sys together sys# open up PDB record and read coordinatespdb_file = open("align_z.pdb", "r")new_pdb_file = open("vac.pdb", "w")#Get last hold atomfor num, heat in enumerate(pdb_file, 1): if "L01" in line: publish num rest 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)


You are watching: Valueerror: mixing iteration and read methods would lose data

*

Once you iterate pdf_file through enumerate you can not iterate the again except invoking pdb_file.seek(0) seek(0) alters the stream position to the beginning

Here"s my modification:

num = 1for heat in pdb_file: num += 1 if "L01" in line: publish num rest pdb_file.seek(0) # go back to the beginning and then it deserve to be iterated againlast_host=int(num)print(last_host-1)for atom in range(0, last_host-1): data = pdb_file.readline() new_pdb_file.write(data)

*

many thanks for contributing response to ridge Overflow!

Please be certain to answer the question. Carry out details and also share your research!

But avoid

Asking for help, clarification, or responding to various other answers.Making statements based on opinion; ago them up with referrals or personal experience.

To discover more, view our advice on writing good answers.


article Your prize Discard

By click “Post her Answer”, girlfriend agree to our regards to service, privacy policy and cookie plan


Not the prize you're feather for? Browse various other questions tagged python for-loop enumerate or ask your own question.


exactly how to read multiple records stored in a given folder and also fetch certain date value type all records -- Python
*

site style / logo © 2021 stack Exchange Inc; user contributions licensed under cc by-sa. Rev2021.11.16.40766




See more: Mason Woodard Funeral Home Joplin Mo

Stack Overflow works best with JavaScript enabled
*

her privacy

By clicking “Accept all cookies”, friend agree ridge Exchange have the right to store cookies on your machine and disclose details in accordance v our Cookie Policy.