背景
- ZipFileのopenメソッドを使ったwriteとwritestrメソッドを使った書き込みの確認
考えたこと
- openメソッドのdefault modeはreadで、基本的には書き込みにはwritestr使えば良さそう。
- https://docs.python.org/ja/3/library/zipfile.html?highlight=zipfile#zipfile.ZipFile.open
コード
import zipfile
content = 'content'
def zip_file_open():
zipfilename = 'open.zip'
with zipfile.ZipFile(zipfilename, mode="w") as zip_file:
with zip_file.open('open.csv', 'w') as f:
f.write(content.encode())
def zip_file_writestr():
zipfilename = 'writestr.zip'
with zipfile.ZipFile(zipfilename, mode="w") as zip_file:
zip_file.writestr('writestr.csv', content)