Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.txt文件在从python脚本写入后为空--/--传递解密密钥_Python_Encryption_Blowfish - Fatal编程技术网

.txt文件在从python脚本写入后为空--/--传递解密密钥

.txt文件在从python脚本写入后为空--/--传递解密密钥,python,encryption,blowfish,Python,Encryption,Blowfish,我正在尝试创建一个脚本,用于从充当密码管理器的.txt文件中附加/更改/检索密码 在研究此处所问问题的历史记录时,大多数用户似乎都会忘记flush()或close()文件流 theList = {} # Populates theList with pairs def getData(): with open('pass_data.txt', 'w+') as data: for line in data: keyValuePair = line

我正在尝试创建一个脚本,用于从充当密码管理器的.txt文件中附加/更改/检索密码

在研究此处所问问题的历史记录时,大多数用户似乎都会忘记
flush()
close()
文件流

theList = {}

# Populates theList with pairs
def getData():
    with open('pass_data.txt', 'w+') as data:
        for line in data:
            keyValuePair = line.split()
            theList[keyValuePair[0]] = keyValuePair[1]

# Writes pairs from theList to file 'pass_data.txt'
def putData():
    with open('pass_data.txt', 'w+') as data:
        for k, v in theList.items():
            data.writelines([k, ' ', v, "\n"])
        data.flush()

# Appends new pair to theList or overwrites if already exists
def setPass(service, password):
    getData()
    theList[service] = password
    putData()

# Retrieves password from given key 'service'
def getPass(service):
    getData()
    print theList[service]
在阅读了这个问题之后,我添加了
flush()
,这没有什么区别…my.txt仍然是空的,在从命令行调用函数时,我无法
getPass('service')
,因为它说没有键值“service”


我还有第二个问题,也许有人能在这里为我回答。如果我使用vim
vim-u~/.vimrc\u encrypted-x pass\u data.txt中的河豚对pass\u data.txt文件进行加密,有没有办法将解密文件的密钥作为
setPass()
getPass()
的参数传递给:

模式“r+”、“w+”和“a+”打开文件进行更新(读取和 写作);请注意,“w+”会截断文件。将“b”附加到模式以 在不同的系统上,以二进制模式打开文件 二进制和文本文件;在没有这种区别的系统上, 添加“b”无效

getData
中,您基本上是在读取文件之前将其截断,因此它看起来是空的。

来自:

模式“r+”、“w+”和“a+”打开文件进行更新(读取和 写作);请注意,“w+”会截断文件。将“b”附加到模式以 在不同的系统上,以二进制模式打开文件 二进制和文本文件;在没有这种区别的系统上, 添加“b”无效

getData
中,您基本上是在读取文件之前将其截断,因此它看起来是空的。

来自:

模式“r+”、“w+”和“a+”打开文件进行更新(读取和 写作);请注意,“w+”会截断文件。将“b”附加到模式以 在不同的系统上,以二进制模式打开文件 二进制和文本文件;在没有这种区别的系统上, 添加“b”无效

getData
中,您基本上是在读取文件之前将其截断,因此它看起来是空的。

来自:

模式“r+”、“w+”和“a+”打开文件进行更新(读取和 写作);请注意,“w+”会截断文件。将“b”附加到模式以 在不同的系统上,以二进制模式打开文件 二进制和文本文件;在没有这种区别的系统上, 添加“b”无效


getData
中,您基本上是在读取文件之前将其截断,因此它看起来是空的。

太好了,非常感谢!这解决了我的问题,但为了第二个问题的可见性,我将在几个小时内将其标记为未解决。太好了,非常感谢!这解决了我的问题,但为了第二个问题的可见性,我将在几个小时内将其标记为未解决。太好了,非常感谢!这解决了我的问题,但为了第二个问题的可见性,我将在几个小时内将其标记为未解决。太好了,非常感谢!这解决了我的问题,但为了第二个问题的可见性,我将在几个小时内将其标记为未解决。