I'm trying to create multiple folders in my current directory so I'm using os.mkdir()
to do that. The problem is that this function returns an error if the folder already exists so I created a try
/except
block.
I would like it to skip the line if it encounters an error. The problem is that this would require one block per directory as I want it to check every single one of them individually.
I do realize that there probably exists another function that doesn't return an error, but I just want to know if it's possible to tell the computer to keep reading the block and simply skip any line that gives it an error, but not any other line.
So if I want to create dir1
to dir5
, but dir3
already exists, having something like:
try:
os.mkdir('dir1')
os.mkdir('dir2')
os.mkdir('dir3')
os.mkdir('dir4')
os.mkdir('dir5')
except:
pass
I would like it to still create directory 1 through 5 instead of only creating 1 and 2. How can I achieve this?