Why doesn't Python write to the file? -
i have programmed small password generator can save password , service on file named "password.txt". everytime run program, file remains blank. when delete file , run program again, file "password.txt" created still blank.
import random letters = "1 2 3 4 5 6 7 8 9 q w e r t z u o p s d f g h j k l y x c v b n m q w e r t z u o p s d f g h j k l y x c v b n m : ; , . 0".split() def checknumb(string): in string: x = i.isdigit() if x == true: return true def createpassword(): global letters password = "" = 0 passlength = random.randint(7, 10) while < passlength: passletter = random.choice(letters) password += passletter += 1 x = checknumb(password) if x != true: password = createpassword() return password print("what name of service?") service = input() password = createpassword() print(password, "will password for", service) file = open("password.txt","a") file.write(service + ": " + password) file.close()
also, because beginner, great if point out few improvements can in code.
edit : indentation error on while statement mistake made while copying code here. program has no indentation errors. asongtoruin reminding me.
just small indentation error
import random letters = "1 2 3 4 5 6 7 8 9 q w e r t z u o p s d f g h j k l y x c v b n m q w e r t z u o p s d f g h j k l y x c v b n m : ; , . 0".split() def checknumb(string): in string: x = i.isdigit() if x == true: return true def createpassword(): global letters password = "" = 0 passlenght = random.randint(7, 10) while < passlenght: passletter = random.choice(letters) password += passletter += 1 x = checknumb(password) if x != true: password = createpassword() return password print("what name of service?") service = input() password = createpassword() print(password, "will password for", service) file = open("password.txt","a") file.write(service + ": " + password) file.close()
this works, @ while indentation in createpassword()
output
python write_file.py name of service? service1 scawelnu7f password service1
file content:
service1: uj.vosk38
also refer link, file appending difference
Comments
Post a Comment