0

Basically I am working on a random password generator and I wanted a way to add the passwords that are generated into a .txt file.

Below is my current code.

import random
letters = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")  # The list of letters, numbers and special characters that will be used in the generation.
numbers = list("1234567890")
special_characters = list("!@#$%^&*()")
characters = list("letters" + "numbers" + "special_characters")


def generate_random_password():
    length = int(input("Enter password length: "))
    letter_count = int(input("How many letters do you want in your password? "))
    number_count = int(input("How many numbers do you want? "))
    special_character_count = int(input("How many special characters do you want? "))
    character_count = letter_count + number_count + special_character_count
    if character_count > length:
        print(
            "Character count surpasses the set length, please try again.")  # Informs user that they made the total character count longer than the designated length for the password.
        return
    userpassword = []
    for i in range(letter_count):
        userpassword.append(random.choice(letters))
    for i in range(letter_count):
        userpassword.append(random.choice(numbers))
    for i in range(special_character_count):
        userpassword.append(random.choice(special_characters))
    if character_count < length:
        random.shuffle(characters)
        for i in range(length - character_count):
            userpassword.append(random.choice(characters))
    random.shuffle(userpassword)
    print("".join(userpassword))
generate_random_password() #The final command that generates the password

`

Requiem
  • 1
  • 1
  • Does this answer your question? [Print string to text file](https://stackoverflow.com/questions/5214578/print-string-to-text-file) – Alexander Oct 26 '22 at 06:20
  • Hi, welcome to SO. Just to check: what problem are you having? Do you need to know how to write to a file? – John M. Oct 26 '22 at 06:20

2 Answers2

0

First of all it's much better to return a value from a function than to print it:

def generate_random_password():
    length = int(input("Enter password length: "))
    ...
    random.shuffle(userpassword)
    return "".join(userpassword)

After that all you have to do is to open a file for appending and write to it:

with open("filename.txt","a") as myfile:
    myfile.write(generate_random_password())
0

You could try something like below:

import random
letters = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")  # The list of letters, numbers and special characters that will be used in the generation.
numbers = list("1234567890")
special_characters = list("!@#$%^&*()")
characters = list("letters" + "numbers" + "special_characters")


def generate_random_password():
    length = int(input("Enter password length: "))
    letter_count = int(input("How many letters do you want in your password? "))
    number_count = int(input("How many numbers do you want? "))
    special_character_count = int(input("How many special characters do you want? "))
    character_count = letter_count + number_count + special_character_count
    if character_count > length:
        print(
            "Character count surpasses the set length, please try again.")  # Informs user that they made the total character count longer than the designated length for the password.
        return
    userpassword = []
    for i in range(letter_count):
        userpassword.append(random.choice(letters))
    for i in range(letter_count):
        userpassword.append(random.choice(numbers))
    for i in range(special_character_count):
        userpassword.append(random.choice(special_characters))
    if character_count < length:
        random.shuffle(characters)
        for i in range(length - character_count):
            userpassword.append(random.choice(characters))
    random.shuffle(userpassword)
    # Make a new variable for our output here
    output = "".join(userpassword)
    print(output)
    # Return the value from above here, so that we can use it elsewhere
    return output

results = generate_random_password() #The final command that generates the password

# Write the results variable to a text file named 'Output'
with open("Output.txt", "w+") as text_file:
    text_file.write(results)

Below are a few sample arguments that you can use to replace the "w+" value from above, depending on the behavior you want:

'''
w  write mode
r  read mode
a  append mode

w+  create file if it doesn't exist and open it in (over)write mode
    [it overwrites the file if it already exists]
r+  open an existing file in read+write mode
a+  create file if it doesn't exist and open it in append mode
'''

The thread below may help explain writing to a text file a bit more:

Writing to a new file if it doesn't exist, and appending to a file if it does

jrynes
  • 187
  • 1
  • 9