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?

Srivatsan
  • 9,225
  • 13
  • 58
  • 83
  • Have you even *tried* to make those directories? Where did you get stuck? – jonrsharpe Jul 07 '15 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`. – Srivatsan Jul 07 '15 at 11:12
  • And you aren't off Googling *"python create directory"* because...? – jonrsharpe Jul 07 '15 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? – Srivatsan Jul 07 '15 at 11:14
  • http://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python – liushuaikobe Jul 07 '15 at 11:15
  • Given that `mkdir` just takes a string, the name of the directory, which you're *already creating in your function*, what's the problem?! *"I don't find any solutions for my specific case"* - so you take the general solution and adapt it to your specific case (or *"do programming"*, as it's known). – jonrsharpe Jul 07 '15 at 11:15
  • @jonrsharpe: So before I save the file, I should call `os.mkdir(path name with function params)` and then save it, right? – Srivatsan Jul 07 '15 at 11:17
  • 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. – jonrsharpe Jul 07 '15 at 11:17
  • @jonrsharpe: actually yes, I shall give that a go. – Srivatsan Jul 07 '15 at 11:19

1 Answers1

1

You can create the directory with :

os.mkdir

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

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48