Skip to content

Cross-platform path handling: str(Path) breaks shell commands on Windows #3637

@lauraporta

Description

@lauraporta

Is your feature request related to a problem? Please describe.

I'm programmatically generating Snakemake input and output paths in rules using Python’s pathlib.Path with str(path), which works well on Unix-based systems. However, on Windows, it inserts backslashes (e.g., C:\data\file.tif), which is expected, .as_posix() is the correct method to use there. This is a problem for me, as I want to use the same Snakefile across different operating systems.

Describe the solution you'd like

I suggest internally normalizing paths based on the operating system by substituting slashes as needed. Paths could be written using the str(path) method and then corrected under the hood by Snakemake.

Describe alternatives you've considered

I currently work around this using:

path.as_posix() if os.name == "nt" else str(path)

This reliably produces shell-safe path strings across platforms.

Additional context

It's important to me that the workflow runs reliably across different operating systems. I think this subtle behavior can easily confuse users working on cross-platform pipelines.

Let me know if I'm misunderstanding how Snakemake handles paths internally or if there's already a recommended approach I'm missing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions