Jan-31-2018, 03:45 PM
Hi -
I wrote a simple py script to basically act as a password json repository. Yes, I'm aware I could accomplish this is many different ways (even with a simple text file instead of a json file), but I wanted to practice my json syntax and regex.
Basically, the script has 3 parts.
-1st asks for some site (this is the key in the json dict) and then for the password value.
-2nd The json file should open and a simple re.search looks to match the password entered with some json element. It prints the site name and the password
-3rd if none is found the script appends the site and the password to the json file.
here is my script
[inline]
Traceback (most recent call last):
File "pwkpr0.py", line 69, in <module>
pull_file()
File "pwkpr0.py", line 37, in pull_file
file_contents = json.load(f)
[/inline]
I figured I'd have some debugging to do, but I couldn't figure this out.
I wrote a simple py script to basically act as a password json repository. Yes, I'm aware I could accomplish this is many different ways (even with a simple text file instead of a json file), but I wanted to practice my json syntax and regex.
Basically, the script has 3 parts.
-1st asks for some site (this is the key in the json dict) and then for the password value.
-2nd The json file should open and a simple re.search looks to match the password entered with some json element. It prints the site name and the password
-3rd if none is found the script appends the site and the password to the json file.
here is my script
#!/usr/bin/env python3 #pwkpr0.py import json, os, sys, random from functools import partial os.chdir('/home/me/Desktop') print(os.getcwd()) def askme(question,type,errmsg): while True: the_input = input(question) if the_input !='': try: the_input = type(the_input) except ValueError: print(f'{the_input} {errmsg}') continue else: print(f'{the_input} is acceptable...') return the_input else: print('can\'t be left empty. Try again.') continue your_info = {} ask_away = partial(askme,type=str, errmsg='is not a string') def pull_file(): get_site = ask_away('enter site') get_info = ask_away('password for site') filen='pwholder.json' with open(filen,'a+') as f: file_contents = json.load(f)#code snags here...is any thing wrong with this syntax? print(file_contents) pull_up(file_contents) choice = ask_away('type \'again\' to start again\n\'leave\' to leave.') if choice == 'again' or choice == 'a': pull_file() else: print('thank you for using pwkpr...best of luck not getting hacked...') print('logging out in 2 secs...') sys.exit() def pull_up(file_contents): get_site get_site.strip().lower() locate = re.search(r'get_site',file_contents)#search for get_site value in file_contents if locate: print(f'your {get_site} password is: {your_info[get_site]}') else: print('no entry found...we\'ll make one.') make_entry() #filen is theoretically open when make_entry() is called... #bc filen stays open for duration of pull_up() def make_entry(): print('In a few moments, you will be asked for this site\'s password') sys.sleep(2) get_info your_info[get_site] = get_info json.dump(your_info,f) if __name__=='__main__': print('we can begin...') pull_file() else: print(f'sorry {__name__} can\'t be imported...')my traceback
[inline]
Traceback (most recent call last):
File "pwkpr0.py", line 69, in <module>
pull_file()
File "pwkpr0.py", line 37, in pull_file
file_contents = json.load(f)
[/inline]
I figured I'd have some debugging to do, but I couldn't figure this out.