1. 读写 zip
相关模块安装可以参考 [Python] 读取 rar/zip 压缩文件
import zipfile
def ls_zip(zip_file_path):
'''获取zip压缩文件夹dir'''
_zip = zipfile.ZipFile(zip_file_path)
zip_dir = _zip.namelist()
return zip_dir
def read_zip(zip_file_path, csv_file_name, header=None, dtype=None, low_memory=False):
'''读取zip压缩文件夹中的某个csv文件'''
try:
_zip = zipfile.ZipFile(zip_file_path)
data = _zip.open(csv_file_name)
df = pd.read_csv(data, header=header, dtype=dtype, low_memory=low_memory)
return df
except Exception as e:
raise Exception(str(e))
def to_zip(to_zip_file, zip_file, is_python=False):
'''将指定文件或文件夹压缩为zip文件'''
if is_python:
with zipfile.ZipFile(zip_file,"w",zipfile.ZIP_DEFLATED) as f:
f.write(to_zip_file)
elif not is_python:
zip_file_dir = os.path.split(to_zip_file)[0]
os.system(f'cd {
zip_file_dir}')
os.system(f'zip -rq {
zip_file} {
to_zip_file