0

Suppose I have a function:

def function(a,b,c):
    do some stuff
    array = some calcualated values
    np.savetxt('/home/user/'+str(a)+'<z<'+str(b)+'/mass/M_'+str(c)+'.dat',array)

As you can see I am saving a file called M_+str(c)+.dat in the path /home/user/'+str(a)+'<z<'+str(b)+'/mass/.

The problem I have is that, both str(a)+'<z<'+str(b) and mass folders don't exist.

I have to create them inside the function and save the file inside these folders. How do I achieve this task?

9
  • Have you even tried to make those directories? Where did you get stuck? Commented Jul 7, 2015 at 11:10
  • @jonrsharpe: to be frank, I don't know how to do it!! When I just run the function with a,b,c parameters, I get the obvious error No such file or directory. Commented Jul 7, 2015 at 11:12
  • And you aren't off Googling "python create directory" because...? Commented Jul 7, 2015 at 11:13
  • @jonrsharpe: I did, but I don't find any solutions for my specific case. I can use os.mkdir, but how do I do it when I need the directory names to be based on my function paramters? Commented Jul 7, 2015 at 11:14
  • 1
    Why don't you try that, and find out? The worst that can happen is that you get a different error message, and then you just try to solve that. Commented Jul 7, 2015 at 11:17

1 Answer 1

1

You can create the directory with :

os.mkdir

See the doc : https://docs.python.org/2/library/os.html

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

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.