+2 votes
in Programming Languages by (75.9k points)

I want to create subfolders inside a folder that does not exist. E.g. foo/bar/bar1

When I use mkdir('foo'), it creates the directory 'foo'. But when I use mkdir('foo/bar'), it gives error.

1 Answer

+1 vote
by (353k points)
selected by
Best answer

Try makedirs() instead of makedir(). It will work.

E.g. To create foo/bar/bar1,

>>> import os
>>> os.makedirs('foo/bar/bar1')