2

The main problem is that after inputting a login program just returns me back instead of working with row in my sorted dictionary. I have a function, that created for registration, Previously I worked with it because I have the full program with autorization and registration. Now I need this function again for another program with autorization, but it doesn't working (but it worked...)

here is my function:

def reg(r) -> None:
    loginnn = input_str("Create login:\n", "login must be made of 4 - 20 symbols")
        with open("hashpy.csv") as f:
            reader = csv.DictReader(f, delimiter=',')
            sorted_dict = sorted(reader, key=lambda k: k['login'])
            for row in sorted_dict:
                #don't worry about code below, it's still under 'construction'
                login, password, salt, role = row:
                passworddd = input_str("Create password:\n", "password must be made of 4 - 20 symbols")
                salt = uuid.uuid4().hex
                print(salt + "salt") 
                passworddd = hashlib.sha256(password.encode() + salt.encode())
                print(passworddd)
                print("Registraion is succeed!")

                with open(file_name2, 'a') as f:
                    f.write(f"\n{loginnn},{passworddd},{salt},{r}") # r == role (r is an argument)  
                return

I used debugger and I saw, that everything is good till program compiling this line: sorted_dict = sorted(reader, key=lambda k: k['login']) which returns me sorted_dict [] After this line program just returns to with open("hashpy.csv") as f: with 0 changes and then returns me back to menu.

slsforme
  • 21
  • 1
  • (1) Try to use an absolute path to "hashpy.csv". A relative path may not point to the expected location. (2) Can you show a sample of "hashpy.csv" as properly formatted text in the question? – Michael Butscher Feb 20 '23 at 01:12
  • (1). After I wrote an absolute path it showing me this: `line 141 with open("C:\Users\User\PycharmProjects\pythonProject52\hashpy.csv") as f ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape` Usually I used just a name of the file and everything was good.Also I have such thing in program: `if not os.path.exists(file_name2): # if file doesn't exists with open("hashpy.csv", 'w') as f: #creating f.write("login,password,salt,role")` – slsforme Feb 20 '23 at 01:31
  • (2) Sorry but what you mean by properly formated text? – slsforme Feb 20 '23 at 01:35
  • (1) Strings with backslashed should be made raw strings by prefix `r`, e. g. `with open(r"C:\Users\User\Pych...`. (2) Formatting the CSV as code is the best approach to retain newlines and characters as original. – Michael Butscher Feb 20 '23 at 02:55
  • I added prefix r and yeah program started working, but problem isn't fixed. Nothing changed. – slsforme Feb 20 '23 at 10:14
  • @slsforme can you include a sample from "hashpy.csv"? Either open it in notepad and copy the first 5 lines or so *or* copy whatever is printed from `with open("hashpy.csv") as f: print('\n'.join(f.read().splitlines()[:5]))` and paste into your question. – Driftr95 Feb 20 '23 at 20:26

0 Answers0