Snakemake provides access to a workflow object within a Snakefile. This allows one to, for example, have dynamic programmatic access to the directory containing the Snakefile (via the workflow.basedir attribute). Is there a similar way to access the working directory that is specified on the command line using --directory?

Through a bit of exploration I was able to figure out that the workflow object has a workdir() method, but it appears this is used internally by Snakemake for registering working directories, not for access.

UPDATE: It looks like the working directory is stored internally as the _workdir attribute of the workflow object. However, this attribute is unset (default value None) when I'm creating static/global objects in the Snakefile. Is it impossible to access the working directory prior to building the workflow DAG?


You could query Python for the working directory within the Snakefile, since evidently Snakemake changes the actual working directory of the process to the one specified by --directory. For example, using a stub Snakefile with just these two lines:

import os
print("Current working directory: " + os.getcwd())

And on the command line:

$ snakemake --directory working-directory
Creating specified working directory working-directory.
Current working directory: /home/jesse/dev/snakemake-wd/working-directory
Building DAG of jobs...
Nothing to be done.
Complete log: /home/jesse/dev/snakemake-wd/working-directory/.snakemake/log/2019-08-02T142009.349338.snakemake.log
  • $\begingroup$ Thanks! This is the type of thing that seems super obvious after you've pointed it out, but wasn't obvious initially. $\endgroup$ Aug 2 '19 at 20:49
  • $\begingroup$ No problem. I wasn't sure myself if that was how they implemented --directory until I tried it out. I guess it's an implementation detail but seems unlikely to me it'll change. $\endgroup$
    – Jesse
    Aug 2 '19 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.