Babar Ali
Babar Ali

Reputation: 71

For Loop in python x number of times from a file

The script runs for the number of accounts that are present in the accounts.txt file. I want to run the script x number of times, no matter how many accounts are present in the accounts.txt file. So I just enter an input 10 and the script should run the for loop for only 10 times. Below is my code.

Can someone please help me as to how to fix the for loop or add a new parent for the for loop?

file = open('accounts.txt','r')

for line in file:
    credentials = line.split(";")
    username = credentials[0]
    password = credentials[1]
    comment = credentials[2]

    chromedriver = "/Users/Ali/Downloads/chromedriver"

    os.environ["webdriver.chrome.driver"] = chromedriver
   # driver = webdriver.Chrome(chromedriver)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--mute-audio")

Upvotes: 2

Views: 463

Answers (3)

JohanL
JohanL

Reputation: 6891

Another way to solve this problem is to use itertools.islice() which allows you to pick exactly as many values from an iterator (e.g a file pointer) that you want:

import itertools

with open('accounts.txt', 'r') as file:
    wanted_lines = 10
    for line in itertools.islice(file, wanted_lines):
        credentials = line.split(";")
        username = credentials[0]
        password = credentials[1]
        comment = credentials[2]

        chromedriver = "/Users/Ali/Downloads/chromedriver"

        os.environ["webdriver.chrome.driver"] = chromedriver
       # driver = webdriver.Chrome(chromedriver)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--mute-audio")

Here, in addition, I have used the with open... construction to open the file to read. The benefit of doing this, is that the file will automatically close when reaching the end of the with block.

Upvotes: 0

Ubdus Samad
Ubdus Samad

Reputation: 1215

Put the reading thing in a function :

def run_loop():
    file_han = open('accounts.txt','r')
    file = file_han.read().split('/n') #you need to read and split the file w.r.t lines
    file_han.close()
    for line in file:
        credentials = line.split(";")
        username = credentials[0]
        password = credentials[1]
        comment = credentials[2]

        chromedriver = "/Users/Ali/Downloads/chromedriver"

        os.environ["webdriver.chrome.driver"] = chromedriver
        # driver = webdriver.Chrome(chromedriver)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--mute-audio")

#Now loop the function over and over
i=0
while i =! input('Enter iterations no:'):
    run_loop()
    i=i+1

Upvotes: 0

jacoblaw
jacoblaw

Reputation: 1283

this should work:

for count, line in enumerate(file):
    credentials = line.split(";")
    username = credentials[0]
    password = credentials[1]
    comment = credentials[2]

    chromedriver = "/Users/Ali/Downloads/chromedriver"

    os.environ["webdriver.chrome.driver"] = chromedriver
   # driver = webdriver.Chrome(chromedriver)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--mute-audio")

    if count == 9: # count starts at 0
        break

Upvotes: 5

Related Questions