Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Loop for n iterations-Python

I have a simple for loop in a Python script:

for filename in filenames:
    outline = getinfo(filename)
    outfile.write(outline)

This for loop is part of a larger script that extracts data from HTML pages. I have nearly 6GB of HTML pages and want to do some test runs before I try it on all of them.

How can I make the loop break after a set number of iterations (lets say 100)?

like image 273
user2475523 Avatar asked May 30 '26 04:05

user2475523


1 Answers

for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)

The list slice filenames[:100] will truncate the list of file names to just the first 100 elements.

like image 89
kqr Avatar answered May 31 '26 18:05

kqr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!