Search code examples
pythonfunctiondirectorycreation

Creating a directory based on function parameters, Python


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?


Solution

  • You can create the directory with :

    os.mkdir
    

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