Today, I would like to explain how to create a function that archives files.
Now, I have to add signatures and data(file name and content) to my buffer(arc_buf). Also, I have to do that for each file.
def archive(file_list): for element in file_list: for bt in new_file_bytes: arc_buf.append(bt) for bt in file_name_bytes: arc_buf.append(bt) for bt in element: arc_buf.append(ord(bt)) for bt in file_content_bytes: arc_buf.append(bt)
To read binary data from files, I can use “with open(element,”rb”) as f:”.
with open(element,"rb") as f: f_buf = f.read() for bt in f_buf: arc_buf.append(bt)
So, I want to save my buffer to a file. To do that I can also use open() function but now with mode “wb”.
with open(file_list + ".ca","wb") as f: f.write(arc_buf) print("DONE")
open() function modes:
“w” – write text to a file
“r” – read text from a file
“wb” – write binary data to a file
“rb” – read binary from a file
More modes here: https://www.w3schools.com/python/ref_func_open.asp