Python write b'xxxx' to config and read it

1.5k views Asked by At

i have encrypted a password and the result is like this: b'&Ti\xcfK\x15\xe2\x19\x0c' i want to save it to an config file and reload it so i can decrypt it and i can use it again as password

4

There are 4 answers

0
AudioBubble On BEST ANSWER
# To save it:
with open('file-to-save-password', 'bw') as f:
    f.write( b'&Ti\xcfK\x15\xe2\x19\x0c')

# To read it:
with open('file-to-save-password', 'br') as f:
    print(f.read())
0
Zach Gates On

Take a look at Python's open builtin function.

with open('foo.txt', 'wb') as f:
    f.write(b'&Ti\xcfK\x15\xe2\x19\x0c')
0
Jordan Gregory On

You can do something like this:

# to write the file

cryptpw = "your encrypted password"

config = open("/path/to/config/file/pw.config","w")
config.write(cryptpw)
config.close()

# to reopen it

config = open("/path/to/config/file/pw.config","r")
print(config.read())
config.close()

It's up to you what you do with the contents of that file, i just chose to print it.

0
Rich Tier On

python persistence is useful here. e.g:

import shelve

with shelve.open('secrets') as db:
    db['password'] = b'&Ti\xcfK\x15\xe2\x19\x0c'