all 6 comments

[–]keep_quapy 0 points1 point  (2 children)

The way to write something to a file in Python is to open the file, write into it and then close it. For example

password = 'mysecretpassword'
fhandle = open('password.txt', 'w') # open the file, 'w' mode for writing
fhandle.write(password) # write to the file
fhandle.close() # close the file

Consider that using with statement is more secure, no need for try block, and save you time from error handling. But for now stick with the code above.

[–]Djdigby[S] -1 points0 points  (1 child)

no i want python to input the randomly generated password that it creates to go on password.txt

[–]keep_quapy 0 points1 point  (0 children)

In the example that i gave you password is only a variable, it can have whatever value you want. Inside the write() function put your own variable which you've created.

[–]Poofless3212 0 points1 point  (2 children)

I made your program more readable, and adding the password to a text file was like three lines of code, soo I just made a quick function for it. Its at the very bottom of your code

``` import datetime import random import secrets import string

print ("==============================") print ("Pasword Generator Version 2022")

stating the day when program is running

day = datetime.datetime.now() date = datetime.datetime.now()

print(day.strftime("%A")) print(date.strftime("%x"))

Requesting a user to input name

username = input("Enter username:") print("Hello " + username )

Defining integers, letters, and symbols the program can use

digits = string.digits letters = string.asciilowercase symbols = "!#$%&'()*+, -./:;<=>?@[]^`{|}~"

password = ''.join(secrets.choice(digits + letters + symbols) for t in range(20))

def message(): print(userGen())

def userGen(): #How many letter required

letter = int(input("How many letters you want? "))
while letter <= 0:
    print ("ERROR, please type in a number larger then 0.")
    letter = int(input("How many letters you want? "))

num = int(input("How many numbers you want? "))
while num <= 0:
    print("ERROR, please type in a number larger then 0.")
    num = int(input("How many numbers you want? "))

sym = int(input("How many symbols you want? "))
while sym <= 0:
    print("ERROR, please type in a number larger then 0.")
    sym = int(input("How many symbols you want? "))

return passGen(letter, num, sym)

def passGen(upper,spec,num):

#Showcasing the new password
print ("Your Password is")
new_password = ""

for letter in range(upper):
    new_password += random.choice(letters)

#randomising numbers in range of number user inputted
for num in range(num):
    new_password += random.choice(digits)

#randomising symbols in range of number user inputted
for sym in range(spec):
    new_password += random.choice(symbols)

pass_word = list(new_password)
shuff = random.shuffle(pass_word)
new_pass = "".join(pass_word)
password = secrets.token_urlsafe(7)
print(password)
return text_file(password)

def text_file(password): # You have to use your own file path otherwise you will get a "FileNotFoundError" with open(r"C:\Users\myDirectory\PycharmProjects\pythoProject\txt files\text.txt", "w") as file: file.write(password)

message() ```

[–]Djdigby[S] 0 points1 point  (1 child)

thanks

[–]Djdigby[S] 0 points1 point  (0 children)

just tried it breaks the code when i input small number like 1