pythonlistgenerateword-list

Generate a sorted list of birthday dates and append each date to a newline in a file


So, I have been trying to generate a wordlist with birthday dates. I am trying to append each value to a newline in a file birdthday_wordlist.txt. The file and the format should be like this:

01/01/1998
02/01/1998
03/01/1998
dd/mm/yyyy
12/12/2000

I was capable of generating only the dd, mm or yyyy, with scripts like this:

with open('XXXXXX_wordlist.txt', 'w') as birdthday_wordlist:
for i in range(1980, 2000):
    birdthday_wordlist.write('{}\n'.format(i))

I know there is a way, for now I couldn't figure it out.


Solution

  • If I understand what you're asking, it's very similar to the question here

    I have adapted the answer to write the dates to a file:

    from datetime import timedelta, date
    
    def daterange(start_date, end_date):
        for n in range(int ((end_date - start_date).days)):
            yield start_date + timedelta(n)
    
    start_date = date(1980, 1, 1)
    end_date = date(2000, 1, 1)
    with open('XXXXXX_wordlist.txt', 'w+') as birdthday_wordlist:
        for single_date in daterange(start_date, end_date):
            birdthday_wordlist.write('%s\n' % single_date.strftime("%d/%m/%Y"))
    

    Will output:

    01/01/1980
    02/01/1980
    03/01/1980
    04/01/1980
    05/01/1980
    ...
    31/12/1999