patternpythonModerate
Generating a password to save to a .txt file
Viewed 0 times
filepasswordsavegeneratingtxt
Problem
Here is my basic password generator which saves your password to a .txt file which you can choose the name of etc.
This will make a .txt file with a line such as:
Your password for IBM bluemix is: 451juoxQYky
How can I make it 'stronger'? Are there any major holes in it? How can I make it more compact?
This is just a personal project I'm doing to improve my python knowledge.
import random
import string
import os.path
#random numbers section
num1 = random.randrange(100,999)
number_section = num1
#random letters section
string.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()'
let1 = random.choice(string.letters)
let2 = random.choice(string.letters)
let3 = random.choice(string.letters)
let4 = random.choice(string.letters)
let5 = random.choice(string.letters)
let6 = random.choice(string.letters)
let7 = random.choice(string.letters)
let8 = random.choice(string.letters)
letters_section = let1 + let2 + let3 + let4 + let5 + let6 + let7 + let8
#letters and numbers combination
password = str(number_section) + letters_section
#input section
password_for = raw_input('This password is for: ')
your_pass = 'Your password for' + ' ' + password_for + ' ' + 'is:' + ' ' + password
print your_pass
#save section
save_path = 'C:\Users\Charles\Desktop\Passes'
name_of_file = raw_input("What is the name of the file: ")
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName, "w")
toFile = your_pass
file1.write(toFile)
file1.close()This will make a .txt file with a line such as:
Your password for IBM bluemix is: 451juoxQYky
How can I make it 'stronger'? Are there any major holes in it? How can I make it more compact?
This is just a personal project I'm doing to improve my python knowledge.
Solution
By making it more Pythonic for one:
should be:
Now instead of a hard coded length you can ask the user for a length or easily change it by changing one variable instead of adding or deleting lines.
Instead of this:
You want:
The
This seems to be a pattern in your code but its wholly unnecessary:
Just use
So in the end you can simplify to:
Or even more simply:
string.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()'
let1 = random.choice(string.letters)
let2 = random.choice(string.letters)
let3 = random.choice(string.letters)
let4 = random.choice(string.letters)
let5 = random.choice(string.letters)
let6 = random.choice(string.letters)
let7 = random.choice(string.letters)
let8 = random.choice(string.letters)
letters_section = let1 + let2 + let3 + let4 + let5 + let6 + let7 + let8should be:
''.join(random.choice(string.letters) for _ in range(length))Now instead of a hard coded length you can ask the user for a length or easily change it by changing one variable instead of adding or deleting lines.
Instead of this:
file1 = open(completeName, "w")
toFile = your_pass
file1.write(toFile)
file1.close()You want:
with open(completeName, "w") as file1:
file1.write(your_pass) # no need to assign a new variableThe
with context manager will automatically handle closing the file for you.This seems to be a pattern in your code but its wholly unnecessary:
num1 = random.randrange(100,999)
number_section = num1Just use
num1 or save the output of randrange() to number_section directly there is no need for an intermediate variable.So in the end you can simplify to:
import random
import string
import os.path
num1 = random.randrange(100,999)
string.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()'
letters_section = ''.join(random.choice(string.letters) for _ in range(8)) # change 8 to a variable if you want to ask the user for a length or have it passed as an argument
password = str(number_section) + letters_section
password_for = raw_input('This password is for: ')
your_pass = 'Your password for {} is: {}'.format(password_for, password)
print(your_pass)
save_path = 'C:\Users\Charles\Desktop\Passes'
name_of_file = raw_input("What is the name of the file: ")
completeName = os.path.join(save_path, name_of_file+".txt")
with open(completeName, "w") as file1:
file1.write(your_pass)Or even more simply:
def gen_password(length):
choices = string.digits + string.punctuation + string.ascii_letters
return ''.join(random.choice(choices) for _ in range(length))
>>> gen_password(13)
'7P~R6dSy>> gen_password(20)
"Q{qHs6ahwj^EaT_'t,.7"
>>> gen_password(11)
'XDi49z+;^?='Code Snippets
string.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()'
let1 = random.choice(string.letters)
let2 = random.choice(string.letters)
let3 = random.choice(string.letters)
let4 = random.choice(string.letters)
let5 = random.choice(string.letters)
let6 = random.choice(string.letters)
let7 = random.choice(string.letters)
let8 = random.choice(string.letters)
letters_section = let1 + let2 + let3 + let4 + let5 + let6 + let7 + let8''.join(random.choice(string.letters) for _ in range(length))file1 = open(completeName, "w")
toFile = your_pass
file1.write(toFile)
file1.close()with open(completeName, "w") as file1:
file1.write(your_pass) # no need to assign a new variablenum1 = random.randrange(100,999)
number_section = num1Context
StackExchange Code Review Q#101660, answer score: 19
Revisions (0)
No revisions yet.