2017-08-30 119 views
0

我已经加密了密码,结果是这样的:b'& Ti \ xcfK \ x15 \ xe2 \ x19 \ x0c' 我想将它保存到配置文件并重新加载它 所以我可以解密它,我可以再次使用它作为密码Python写入b'xxxx'配置并读取它

回答

1
# 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

看看Python的open内置函数。

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

你可以做这样的事情:

# 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() 

这是给你,你与该文件的内容是什么,我只是选择了将其打印出来。

0

python persistence在这里很有用。例如:

import shelve 

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