Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I would like to print the duration of the script and save it inside the CSV. What would be the best method?

import requests

with open('infile.txt', 'r') as f:

    urls = f.readlines()

datalist=[]

for url in urls:

    data = requests.get(url)

    datalist.append(data.text)

with open('outfile.txt', 'w') as f:

    for item in datalist:

        f.write("%s\n" % item)

1 Answer

0 votes
by (36.8k points)

You can use datetime module.

import requests

from datetime import datetime

def run():

    with open('infile.txt', 'r') as f:

        urls = f.readlines()

    datalist=[]

    for url in urls:

        data = requests.get(url)

        datalist.append(data.text)

    with open('outfile.txt', 'w') as f:

        for item in datalist:

            f.write("%s\n" % item)

startTime = datetime.now()

run()

print(datetime.now() - startTime)

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch.

Browse Categories

...