I'd like to create a folder on the hard-drive for the first time the selected model is exported.

After this, the script will fail, because the folder being created by os.mkdir already exists:

directoryPath = "//_MODEL_FOLDERS_/"
path = bpy.path.abspath(directoryPath)

# fails if dir exists
os.mkdir(path + obj.name)

It should be tested.

So does anyone know of a way to test if a folder already exists before trying to create it?

  • 2
    $\begingroup$ Not sure what this has to do with blender..? $\endgroup$
    – gandalf3
    Sep 29, 2014 at 7:59
  • $\begingroup$ I find it odd that you would make an issue that I did not specifically use the word 'Blender' especially since I did say "to create a folder on the HD for the first time the selected model is exported". $\endgroup$ Sep 29, 2014 at 8:11
  • 1
    $\begingroup$ It sounds to me like you are really asking how to test if a directory exists using python. It doesn't matter whether or not you use the word "Blender", it could be any script you are writing. $\endgroup$
    – gandalf3
    Sep 29, 2014 at 8:15
  • 5
    $\begingroup$ This question appears to be off-topic because it is about pure Python. $\endgroup$
    – CodeManX
    Sep 29, 2014 at 16:16
  • $\begingroup$ Yeah, I did not realize that right away since I only use Python within the context of Blender. $\endgroup$ Sep 29, 2014 at 23:21

1 Answer 1


I would suggest to use os.path.join() instead of +.

To test you can use os.path.exists(os.path.join(path, obj.name))

  • $\begingroup$ Could you explain why you should use os.path.join instead? Also, there's a bracket missing at the end of the last line. $\endgroup$
    – Greg Zaal
    Sep 29, 2014 at 7:55
  • 1
    $\begingroup$ Using + will simply join two strings together while os.path.join() will join two paths "intelligently" which is to say it will place the correct platform specific separator between them if not already present. $\endgroup$
    – sambler
    Sep 30, 2014 at 7:06

Not the answer you're looking for? Browse other questions tagged .