Abdul Rehman
Abdul Rehman

Reputation: 5684

Python Create a nested directory inside a temporary Directory

I'm working on a project using Python(3.7) in which I need to create a subdirectory inside a temporary directory, I have created my temporary directory as:

tempdir = tempfile.mkdtemp()
saved_unmask = os.umask(0o077)
temp_dir = os.path.join(tempdir)

Then I have tried to create a directory within this temp_dir as:

helm_chart = temp_dir + "/helmChart"
subprocess2.call(['helm', 'create', helm_chart])

helm creates path/sub_path always create a directory inside path which is temp_dir in my case, the command above is creating a directory when I passed another directory path, but it's not creating a directory inside temp_dir.

Thanks in advance!

Upvotes: 0

Views: 5002

Answers (2)

ryankdwyer
ryankdwyer

Reputation: 701

This can be solved by doing the following:

import os    
import tempdir

top_level = tempdir.TemporaryDirectory()
nested = tempdir.TemporaryDirectory(dir=top_level.name)

print(nested.name)

This will output /tmp/{top_level_temp_dir}/{nested_temp_dir}

The key is the dir keyword argument. This tells the TemporaryDirectory to use the dir passed in as the base of the new TemporaryDirectory.

Upvotes: 3

alberand
alberand

Reputation: 662

You have saved_unmask = os.umask(0o077), is your script runs under your user? Possibly it doesn't have permission to write to the temporary directory

Upvotes: 0

Related Questions