这是一个trick小的问题。
鉴于以下法典,你将如何采用通用的取用文件方式?
from pathlib import Path
import re
import zipfile as zip
def check_if_zip(path: Path) -> bool:
return path.is_file() and (
re.search(r".zip$", path.name, re.IGNORECASE) is not None
)
sources: list[Path] = [
Path("Foo"), # Directory
Path("Bar.zip"), # Zip
]
for source in sources:
source_is_zip = check_if_zip(source)
# Is there a way to universally wrap each source
# as some kind of "streamed I/O object" or class
# that reads live from either the folder or zip
# file? I need a storage-medium agnostic way
# of fetching files from the "source".
#
# It needs to use a fast byte-stream or similar,
# NOT reading the entire source into memory! :)
# And the stream needs to be seekable so that
# it works as input for other libraries, for
# decoding files contained within the folders/zips.
也许不能在沙尔这样做?
我希望有人会发现这个问题令人感兴趣!