Python Forum
trouble writing to file after while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trouble writing to file after while loop
#6
I rewrote as a class, there were just too many things wronf with the other code
Here's a complete class to get you started.
import json
import os


class NewPassword:
    def __init__(self, filename=None):
        self.usrdatalist = None
        self.filename = filename
        if self.filename is not None:
            if not os.path.exists(filename):
                self.init_file()
            else:
                with open(self.filename, 'r') as f:
                    j = f.read()
                    self.usrdatalist = json.loads(j)

    def init_file(self):
        data = [('user', 'password')]
        with open(self.filename, "w") as f:
            j = json.dumps(data)
            f.write(j)

    def rewrite_file(self):
        with open(self.filename, "w") as f:
            j = json.dumps(self.usrdatalist)
            f.write(j)

    def new_user(self):
        while True:
            username = input("Enter a username: ")
            if username in self.usrdatalist:
                username = input("Name taken, enter new name: ")
                continue
            break
        password = None
        while not password:
            password = input('Enter password: ')
            if len(password) < 12:
                print('Password must be at least 12 characters long')
                password = None
                continue
        self.usrdatalist.append((username, password))
        self.rewrite_file()

def main():
    cp = NewPassword(filename="usrdata.json")
    cp.new_user()

if __name__ == '__main__':
    main()
The password needs to be encoded, double checked, and blanked out while entering
without that, this code is of little use.
If using a gui, you can use getpass (builtin)
Reply


Messages In This Thread
RE: trouble writing to file after while loop - by Larz60+ - Jan-04-2017, 11:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,404 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,029 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,539 Mar-30-2022, 06:25 AM
Last Post: bowlofred
Question Having trouble writing an Enum with a custom __new__ method stevendaprano 3 4,199 Feb-13-2022, 06:37 AM
Last Post: deanhystad
  Writing to file ends incorrectly project_science 4 2,712 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,785 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,460 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Trouble with reading csv file and putting it into a file Milfredo 3 2,292 Sep-04-2020, 05:30 AM
Last Post: Milfredo
  Failure in writing binary text to file Gigux 7 3,844 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,386 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020