💫

pythonで大きなファイルをgzipする

2024/08/24に公開

pythonを使ってcsvファイルをgzipする方法

いまのところうまくいってる処理

import gzip
path = "xxx.csv"
out_path = "yyy.csv"
with open(path, "r", encoding='utf-8') as f_in, gzip.open(out_path, mode='wt',encoding='utf-8') as f_out:
	while True:
		data = f_in.read(1000000)
		if not data:
			break

		f_out.write(date)

たまにエラーが出る処理

import gzip
path = "xxx.csv"
out_path = "yyy.csv"
with open(path, "r", encoding='utf-8') as f_in:
	while True:
		data = f_in.read(1000000)
		if not data:
			break

		with gzip.open(out_path, mode='at', encoding='utf-8') as f_out:
			f_out.write(date)

with gzip.open(out_path, mode='at', encoding='utf-8') as f_out:
このコードだと稀にエラーが発生する
PermissionError: [Errno 13] Permission denied: 'xxx'

GitHubで編集を提案

Discussion