diff --git a/fsspec/core.py b/fsspec/core.py index 6f3652c9a..50c446705 100644 --- a/fsspec/core.py +++ b/fsspec/core.py @@ -210,6 +210,7 @@ def open_files( num=1, protocol=None, newline=None, + auto_mkdir=True, expand=True, **kwargs, ): @@ -248,6 +249,9 @@ def open_files( newline: bytes or None Used for line terminator in text mode. If None, uses system default; if blank, uses no translation. + auto_mkdir: bool (True) + If in write mode, this will ensure the target directory exists before + writing, by calling ``fs.mkdirs(exist_ok=True)``. expand: bool **kwargs: dict Extra options that make sense to a particular storage connection, e.g. @@ -284,6 +288,11 @@ def open_files( protocol=protocol, expand=expand, ) + if fs.protocol == "file": + fs.auto_mkdir = auto_mkdir + elif "r" not in mode and auto_mkdir: + parents = {fs._parent(path) for path in paths} + [fs.makedirs(parent, exist_ok=True) for parent in parents] return OpenFiles( [ OpenFile(