❄️

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

2023/06/22に公開

pythonを使ってファイルを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