+2 votes
in Programming Languages by (15.7k 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

0 votes
by (27.1k points)

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

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

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