Can't listdir a file
Package:
zipp
21
Exception Class:
ValueError
Raise code
def exists(self):
return self.at in self.root._name_set()
def iterdir(self):
if not self.is_dir():
raise ValueError("Can't listdir a file")
subs = map(self._next, self.root.namelist())
return filter(self._is_child, subs)
def __str__(self):
return posixpath.join(self.root.filename, self.at)
Links to the raise (1)
https://github.com/jaraco/zipp/blob/2158b059669a7abfce0a485c647d822a51ca7a9c/zipp.py#L291See also in the other packages (1)
(❌️ No answer)
pipenv/can-t-listdir-a-file/
Ways to fix
Summary: make sure you use a slash at the end of the path your want to iter over:
Code to reproduce (WRONG):
import zipp, io
data = io.BytesIO()
zf = zipp.zipfile.ZipFile(data, 'w')
zf.writestr('fol/file.txt', 'content of a')
p = zipp.Path(zf, 'fol')
print('List of files', list(p.iterdir()))
Working version (Fixed):
import zipp, io
data = io.BytesIO()
zf = zipp.zipfile.ZipFile(data, 'w')
zf.writestr('fol/file.txt', 'content of a')
p = zipp.Path(zf, 'fol/') # <--- HERE IS A FIX WE ADDED SLASH
print('List of files', list(p.iterdir()))
Add a possible fix
Please authorize to post fix