0

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!

2
  • have you tried temp_dir = os.path.join(tempdir, 'helmChart') ? Commented Feb 4, 2019 at 5:47
  • Hi @AsifMohammed, let me give it a try! Commented Feb 4, 2019 at 5:53

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.