I created a password tool in Python.

Introduction

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!

Usage environment

・ Windows 10 ・ VS code 1.51.1

Purpose

Randomly generate three types of passwords in a text file.

code

Please read carefully as it is a long sentence.

code

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))
Results

password.txt


pass
Q2n8d7favk
pass1
PRpnbOuTuj
pass2
0Y1hpyMkea

Commentary

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

I created a password tool in Python.
I created a class in Python and tried duck typing
I made a payroll program in Python!
I want to create a window in Python
I tried playing a typing game in Python
I wrote a class in Python3 and Java
[Memo] I tried a pivot table in Python
I tried adding a Python3 module in C
I tried running alembic, a Python migration tool
I made a Caesar cryptographic program in Python.
A note I looked up to make a command line tool in Python
Take a screenshot in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I wrote python in Japanese
Create a function in Python
Create a dictionary in Python
I want to write in Python! (2) Let's write a test
I tried to implement a pseudo pachislot in Python
I want to randomly sample a file in Python
I want to work with a robot in python.
Make a bookmarklet in Python
I tried to automatically generate a password with Python3
I made a prime number generation program in Python 2
I made a python text
[Python] Created a method to convert radix in 1 second
Easy password box in Python
I understand Python in Japanese!
Draw a heart in Python
What I learned in Python
I tried to implement what seems to be a Windows snipping tool in Python
Publish / upload a library created in Python to PyPI
I implemented a Vim-like replacement command in Slackbot #Python
I used bugspots, a bug prediction tool in mercurial
MALSS, a tool that supports machine learning in Python
I created a stacked bar graph with matplotlib in Python and added a data label
Try running python in a Django environment created with pipenv
I made a simple typing game with tkinter in Python
I tried to implement a one-dimensional cellular automaton in Python
I made a quick feed reader using feedparser in Python
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
I made a puzzle game (like) with Tkinter in Python
I tried to make a stopwatch using tkinter in python
I want to make input () a nice complement in python
I get a UnicodeDecodeError in mecab-python3
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
I made a Line-bot using Python!
Create a DI Container in Python
I get a KeyError in pyclustering.xmeans
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
I made a fortune with Python.
Create a binary file in Python
I wrote Fizz Buzz in Python
Solve ABC036 A ~ C in Python
Write a pie chart in Python