Search code examples
pythonazure-batch

How do I get the path from $AZ_BATCH_NODE_SHARED_DIR?


I am using Azure Batch with Python and I would like to create a directory within the shared space from a batch task.

According to the docs:

Shared: This directory provides read/write access to all tasks that run on a node. Any task that runs on the node can create, read, update, and delete files in this directory. Tasks can access this directory by referencing the AZ_BATCH_NODE_SHARED_DIR environment variable.

Imagine that folder is called test_dir:

if not os.path.exists('test_dir'):
    os.makedirs('test_dir')

Now, what if I want to write a file to that directory? I cannot use:

with open('$AZ_BATCH_NODE_SHARED_DIR/test_dir/test.txt', 'a') as output:
    output.write('hello\n')

How do I get the full path from $AZ_BATCH_NODE_SHARED_DIR?


Solution

  • Use os.environ, which exposes the current environment as a mapping:

    shared = os.environ['AZ_BATCH_NODE_SHARED_DIR']
    with open(os.path.join(shared, 'test_dir', 'test.txt'), 'a') as output: