Skip to content

Commit 51027de

Browse files
authored
Allow append mode for ZIP (#1453)
1 parent 7c3408a commit 51027de

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

fsspec/implementations/local.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ def readlines(self, *args, **kwargs):
386386
def close(self):
387387
return self.f.close()
388388

389+
def truncate(self, size=None) -> int:
390+
return self.f.truncate(size)
391+
389392
@property
390393
def closed(self):
391394
return self.f.closed

fsspec/implementations/tests/test_zip.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,29 @@ def test_zip_glob_star(m):
9494
fs, _ = fsspec.core.url_to_fs("zip::memory://out.zip")
9595
outfiles = fs.glob("*")
9696
assert len(outfiles) == 1
97+
98+
99+
def test_append(m, tmpdir):
100+
fs = fsspec.filesystem("zip", fo="memory://out.zip", mode="w")
101+
with fs.open("afile", "wb") as f:
102+
f.write(b"data")
103+
fs.close()
104+
105+
fs = fsspec.filesystem("zip", fo="memory://out.zip", mode="a")
106+
with fs.open("bfile", "wb") as f:
107+
f.write(b"data")
108+
fs.close()
109+
110+
assert len(fsspec.open_files("zip://*::memory://out.zip")) == 2
111+
112+
fs = fsspec.filesystem("zip", fo=f"{tmpdir}/out.zip", mode="w")
113+
with fs.open("afile", "wb") as f:
114+
f.write(b"data")
115+
fs.close()
116+
117+
fs = fsspec.filesystem("zip", fo=f"{tmpdir}/out.zip", mode="a")
118+
with fs.open("bfile", "wb") as f:
119+
f.write(b"data")
120+
fs.close()
121+
122+
assert len(fsspec.open_files("zip://*::memory://out.zip")) == 2

fsspec/implementations/zip.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ def __init__(
4949
raise ValueError(f"mode '{mode}' no understood")
5050
self.mode = mode
5151
if isinstance(fo, str):
52+
if mode == "a":
53+
m = "r+b"
54+
else:
55+
m = mode + "b"
5256
fo = fsspec.open(
53-
fo, mode=mode + "b", protocol=target_protocol, **(target_options or {})
57+
fo, mode=m, protocol=target_protocol, **(target_options or {})
5458
)
5559
self.of = fo
5660
self.fo = fo.__enter__() # the whole instance is a context

0 commit comments

Comments
 (0)