我正在编写一个程序,通过查看它们的标题来输出目录中的文件类型。
一些文件是压缩的,所以我需要能够解压缩它们作为起点
到目前为止,我已经能够搜索目录并使用标题更改扩展名,打开压缩文件并将其内容存储在变量中,现在我无法将变量保存为新文件。
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)
我猜我将不得不按照f.write("newfile", content)
但不确定的方式使用 a 命令。
提前致谢