It's been about a month since I started studying python, and I created it for the first time. Since the password tool is completed, I wrote an article. There is still a lot of room for improvement, so we will make it even better!
・ Windows 10 ・ VS code 1.51.1
Randomly generate three types of passwords in a text file.
Please read carefully as it is a long sentence.
password.py
import random , string
password = [random.choice(string.hexdigits) for i in range(10)]
password1 = [random.choice(string.hexdigits) for i in range(10)]
password2 = [random.choice(string.hexdigits) for i in range(10)]
passwordlist = ''.join(password)
passwordlist1 = ''.join(password1)
passwordlist2 = ''.join(password2)
passwordlist = str(passwordlist)
passwordlist1 = str(passwordlist1)
passwordlist2 = str(passwordlist2)
passname = 'pass'
passname1 = 'pass1'
passname2 = 'pass2'
import pathlib
pathlib.Path("password.txt").write_text('{}\n{}\n{}\n{}\n{}\n{}\n'.format(passname,passwordlist,passname1,passwordlist1,passname2,passwordlist2))
password.txt
pass
Q2n8d7favk
pass1
PRpnbOuTuj
pass2
0Y1hpyMkea
Then, I will explain.
import random , string
This is importing the random and string functions into python. random: A function that generates random numbers. string: A function for manipulating strings. I actually knew string for the first time this time, but I checked it from the python standard library. python standard library
next,
password = [random.choice(string.hexdigits) for i in range(10)]
This is the code to assign 10 random alphanumeric characters to the variable name: password. random.choice (): Randomly generate what is in (). string.hexdigits: Alphanumeric, mixed case. for i in range (10): Repeat the same process 10 times.
Next, I will summarize a little,
passwordlist = ''.join(password)
passwordlist = str(passwordlist)
passname = 'pass'
passwordlist =''. join (password): Join variable name password and assign to passwordlist passwordlist = str (passwordlist): Convert variable name passwordlist to string passname ='pass': Substitute the string pass for the variable name passname
It is the end.
import pathlib
pathlib.Path("password.txt").write_text('{}\n{}\n{}\n{}\n{}\n{}\n'.format(passname,passwordlist,passname1,passwordlist1,passname2,passwordlist2))
Regarding this, I followed the contents described in the link below as it is. [When there are multiple variables of the string to be written to the file](https://ja.stackoverflow.com/questions/43581/%E3%83%95%E3%82%A1%E3%82%A4%E3%83 % AB% E3% 81% AB% E6% 9B% B8% E3% 81% 8D% E8% BE% BC% E3% 82% 80% E6% 96% 87% E5% AD% 97% E5% 88% 97 % E3% 81% AE% E5% A4% 89% E6% 95% B0% E3% 81% 8C% E8% A4% 87% E6% 95% B0% E3% 81% 82% E3% 82% 8B
that's all. After all, it is difficult to make one program. As a future revision, I want to update every month, so set the update timing Updated automatically. Keep the previous month's password just in case.
I want to make it so far! If you have other ideas or if you can make the code shorter I would be grateful if you could tell me!
Thank you for reading! !!
Recommended Posts