D4Vinci/Scrapling · error · ValueError
Filename must be provided
Error message
Filename must be provided
What it means
write_content_to_file validates that filename is a non-empty, non-whitespace string before use. An empty/None filename, a non-str value, or a whitespace-only string raises ValueError('Filename must be provided'). This fires before the extension check and before the file is opened.
Source
Thrown at scrapling/core/shell.py:665
"\n",
"\r",
"\t",
" ",
):
# Remove consecutive white-spaces
txt_content = TextHandler(re_sub(f"[{s}]+", s, txt_content))
yield txt_content
yield ""
@classmethod
def write_content_to_file(
cls, page: Selector, filename: str, css_selector: Optional[str] = None, main_content_only: bool = False
) -> None:
"""Write a Selector's content to a file"""
if not page or not isinstance(page, Selector): # pragma: no cover
raise TypeError("Input must be of type `Selector`")
elif not filename or not isinstance(filename, str) or not filename.strip():
raise ValueError("Filename must be provided")
elif not filename.endswith((".md", ".html", ".txt")):
raise ValueError("Unknown file type: filename must end with '.md', '.html', or '.txt'")
else:
with open(filename, "w", encoding=page.encoding) as f:
extension = filename.split(".")[-1]
f.write(
"".join(
cls._extract_content(
page,
cls._extension_map[extension],
css_selector=css_selector,
main_content_only=main_content_only,
)
)
)
View on GitHub (pinned to 5d213a2d47)
Solutions
- Default the filename explicitly, e.g. filename = filename or 'output.md'
- Validate required path config before calling (fail fast at the boundary)
- In CLI code, make the output option required=True instead of defaulting to None
Example fix
# before Convertor.write_content_to_file(page, args.output) # args.output is None # after Convertor.write_content_to_file(page, args.output or 'output.html')
Defensive patterns
Strategy: validation
Validate before calling
filename = (filename or '').strip() or 'output.html' assert isinstance(filename, str) and filename, 'filename required'
Type guard
from typing import Any
def is_valid_filename(value: Any) -> bool:
return isinstance(value, str) and bool(value.strip()) Prevention
- Make output-path options required in CLI wrappers instead of optional-and-None
- Default filenames at your boundary before calling library code
When it happens
Trigger: Calling write_content_to_file(page, filename=None) or '' — typically because a path variable from config/argv came through unset, or a f-string built the name from a missing key.
Common situations: Optional CLI args or config keys (output file) not provided while the code forwards them anyway; templated filenames where a substitution evaluated to empty.
Related errors
- Unknown file type: filename must end with '.md', '.html', or
- Unknown extraction type: {extraction_type}
- Invalid JSON data '{json_string}': {err}
- Could not parse cookies '{cookies}': {err}
- Credentials dictionary must contain both 'username' and 'pas
AI-assisted analysis of D4Vinci/Scrapling@5d213a2d47 (2026-08-14).
Data as JSON: /api/errors/af9c317f9c9304df.
Report an issue: GitHub.