Mixing files and loops

后端 未结 4 662
Happy的楠姐
Happy的楠姐 2020-11-29 05:52

I\'m writing a script that logs errors from another program and restarts the program where it left off when it encounters an error. For whatever reasons, the developers of t

4条回答
  •  天命终不由人
    2020-11-29 06:31

    Use for and enumerate.

    Example:

    for line_num, line in enumerate(file):
        if line_num < cut_off:
            print line
    

    NOTE: This assumes you are already cleaning up your file handles, etc.

    Also, the takewhile function could prove useful if you prefer a more functional flavor.

提交回复
热议问题