python解压zip/gz压缩文件到当前目录

1.解压zip

import zipfile
import os
def un_zip(csv_path):
	for f in  os.listdir(csv_path):
		if ".zip" in f:
			zip_file = zipfile.ZipFile(csv_path + "\\" + f)
			zip_file.extract(zip_file.namelist()[0],csv_path)

2.解压gz

def un_gzfile(gz_path):
	# 异常处理
	try:
		# 压缩文件解压
		for f in  os.listdir(gz_path):
			if ".gz" in f:
				g = gzip.GzipFile(mode="rb", fileobj=open(gz_path+"\\"+f, 'rb'))
				open(gz_path+"\\"+f.replace(".gz",""), "wb").write(g.read())
	except Exception as e:
		print(e)
	else:
		print("文件解压成功!")

 

你可能感兴趣的:(Python,python)