0
votes

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!

3
have you tried temp_dir = os.path.join(tempdir, 'helmChart') ? - Asif Mohammed
Hi @AsifMohammed, let me give it a try! - Abdul Rehman

3 Answers

1
votes

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.

0
votes

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

-1
votes

The following might work:

import os
os.makedirs(os.path.join(tempdir, 'helmChart'))