I am writing a program that displays file types inside a directory by looking at their headers.
Some of the files are compressed, so I need to unzip them as a starting point
So far I have managed to search directories and use the title to change the extensions, and open the compressed file and save its contents in a variable, now I can not save the variable as a new file.
def unzip(): os.chdir("C:/Users/David/Myfiles") files = os.listdir(".") for x in (files): f = open((x), "rb") byte1 = f.read(1) byte2 = f.read(1) if byte1 == b'\x1f' and byte2 == b'\x8b': os.rename((x), (x) + ".gz") file = gzip.open((x), "rb") content = file.read() print (content)
I assume that I will have to use the a command line by line f.write("newfile", content) , but not sure.
Thank you in advance
user1816467
source share