Thidal
Thidal

Reputation: 109

os.makedirs() doesnt make the dir instantly, but only after the application closes

This method runs fine, but it doesn't create the dir. Yet when I close my application, only THEN will it create the dir.

text is a name, for example: (Jason)

def addpatient(self, text):
    newpath = os.getcwd() + '/names/' + text + '/'
    if not os.path.exists(newpath):
        os.makedirs(newpath)

Is there something I am missing, or am I using it wrong?

Upvotes: 1

Views: 1297

Answers (1)

tpvasconcelos
tpvasconcelos

Reputation: 727

os.makedirs creates the new directory/directories imediately. Meaning that, if you run

os.makedirs(newpath)
print('newpath created!')

when your reach the second line, the new path has already been created.

So... what I suspect is happpening is that you are running your code in some IDE and that you don't see the new directory being created in your project's directory tree or whatever your IDE has. This can simply bee because your IDE only updates the directory tree every few seconds or only after your program finishes running. Try running this:

import os
import time

def addpatient(text): 
    etc...

addpatient("examplepath")

time.sleep(30)  # go check if examplepath has been created!!

Here, you have created examplepath and then your have "paused" your program for 30 seconds. During these 30 seconds, go check if your directrory has been created.

Another way of checking would be to run:

import os
import time

def addpatient(text): 
    etc...

addpatient("examplepath")

if os.path.exists("examplepath"):
    print("Path exists!")
else:
    print("Path does not exist!")

Let me know if I'm completely wrong and your path is actually not being created!

Upvotes: 1

Related Questions