Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
Python 3.x Pyhton 3不会将附加数据读取到文件中_Python 3.x - Fatal编程技术网

Python 3.x Pyhton 3不会将附加数据读取到文件中

Python 3.x Pyhton 3不会将附加数据读取到文件中,python-3.x,Python 3.x,我正在尝试将二进制数据写入文件。程序将首先检查文件是否存在。如果文件不存在,程序将创建该文件并将数据写入其中。如果确实存在,则数据将附加到文件中。然而,当我试图读取文件时,我无法读取意外数据,只能读取文件创建时写入的数据 def getText(self): self.readKey() st = self.inBox.get('1.0', 'end') fen = Fernet(self.readKey()) encrypted = fen.encrypt(s

我正在尝试将二进制数据写入文件。程序将首先检查文件是否存在。如果文件不存在,程序将创建该文件并将数据写入其中。如果确实存在,则数据将附加到文件中。然而,当我试图读取文件时,我无法读取意外数据,只能读取文件创建时写入的数据

def getText(self):
    self.readKey()
    st = self.inBox.get('1.0', 'end')

    fen = Fernet(self.readKey())
    encrypted = fen.encrypt(st.encode())
    return encrypted

def writeFile(self):

    if (os.path.exists('data.txt') == False):
        file = open('data.txt',mode='wb' )
        file.write(self.getText())
        file.close()
    else:
        file = open('data.txt',mode='ab' )
        #sts = file.read()


        file.write(self.getText())
        file.close()


    self.inBox.delete('1.0','end')




def openFile(self):
    self.outBox.delete('1.0','end')

    fen = Fernet(self.readKey())     

    try:
        f = open("data.txt", mode='rb')
    except:
        alert_popup(self,'Error','No File Exists')


    self.outBox.insert(tk.END, fen.decrypt(f.read()))
使用“a”模式

if (os.path.exists('data.txt') == False):
    file = open('data.txt',mode='a' )
    file.write(self.getText())
    file.close()

您试图在何处读取附加数据?在方法openFile(self)下,可以显示调用函数的代码吗?self.dyc=tk.Button(self,text=“Decrypt”,command=self.openFile)self.dyc.grid(row=1,column=2,pady=2)Ok,但此代码不包括对writeFile的调用。您首先调用openFile还是writeFIle方法?