-1

I am trying to read in multiple csv files that have one column with a list of websites. The column does not have a header. I am trying to use the whois function to pull the creation date of the website. I have created a loop to import the csv file, create a new csv file, read a row, pull the creation date, write the number, website, and creation date to a new csv file, close both csv files, and then repeat the process. However, I am finding that once the first csv file is finished, it is not importing the next one and the code won't finish running. Here is my code:

import whois
import csv 
import os
import time

location = "directory"

old = 'filename_old'
new = 'filename_new'
  
for i in os.listdir(location):
    if i.endswith(".csv"):  
        with open(location + i, "r") as CSVfile:
            CSVfileReader = csv.reader(CSVfile, delimiter = ",")
            CSV2file = open(location + new + i, "a")

            count = 1
            cont = True 

            try: 
                while cont:
                    for row in CSVfileReader:
                        key = row[0].rstrip()
     
                        for links in whois.whois(key).creation_date:
                            print(links)
                            CSVFileWriter = csv.writer(CSV2file)
                            CSVFileWriter.writerow([count, key, links])
                            print('count:', count)
                            count = count + 1
                            time.sleep(0.9)  
            except:
                cont = False
                print("done with csv")

                CSVfile.close()
                CSV2file.close()

else:
    print("The End")

The output reads as this:

runfile('direc')
2019-05-24 01:14:11
count: 1
2019-05-23 20:14:11
count: 2
2009-06-05 17:24:02
count: 3
2009-06-05 10:21:19
count: 4
2006-04-10 21:07:52
count: 5
2006-04-10 16:07:52
count: 6
2019-02-19 22:00:45
count: 7
2019-02-19 17:00:45
count: 8
2001-10-11 23:01:30
count: 9
2001-10-11 18:01:30
count: 10

But it never prints "done with csv" or "the end". Only when I stop it from running will it print that.

Lucan
  • 2,907
  • 2
  • 16
  • 30
MellyJ13
  • 25
  • 1
  • 1
  • 5
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Show where the intermediate results differ from what you expected. In particular, supply your trace of your code's operation, showing where it gets stuck, and values of the suspect variables. – Prune May 06 '21 at 23:52

1 Answers1

0

Your program stucks in while cont: loop.

MarkSouls
  • 982
  • 4
  • 13