Python 将 b'xxxx' 写入配置并读取它
Python write b'xxxx' to config and read it
我加密了一个密码,结果是这样的:b'&Ti\xcfK\x15\xe2\x19\x0c'
我想将它保存到配置文件并重新加载它
所以我可以解密它,我可以再次使用它作为密码
看看 Python 的 open
内置函数。
with open('foo.txt', 'wb') as f:
f.write(b'&Ti\xcfK\x15\xe2\x19\x0c')
# 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())
你可以这样做:
# 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()
这取决于你如何处理那个文件的内容,我只是选择打印它。
python persistence 在这里很有用。例如:
import shelve
with shelve.open('secrets') as db:
db['password'] = b'&Ti\xcfK\x15\xe2\x19\x0c'
我加密了一个密码,结果是这样的:b'&Ti\xcfK\x15\xe2\x19\x0c' 我想将它保存到配置文件并重新加载它 所以我可以解密它,我可以再次使用它作为密码
看看 Python 的 open
内置函数。
with open('foo.txt', 'wb') as f:
f.write(b'&Ti\xcfK\x15\xe2\x19\x0c')
# 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())
你可以这样做:
# 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()
这取决于你如何处理那个文件的内容,我只是选择打印它。
python persistence 在这里很有用。例如:
import shelve
with shelve.open('secrets') as db:
db['password'] = b'&Ti\xcfK\x15\xe2\x19\x0c'