How can I make verifiy error with os.makedirs in Python?

How can I make a validation error for this command?

if blablablabla: os.makedirs('C:\\test\\') 

If the folder already exists, it returns an error to me ... how can I make it ignore this error? and move on?

+6
python
source share
2 answers
 try: os.makedirs('C:\\test\\') except OSError: pass 

You may also need to check for a specific error "already exists" (since OSError may mean other things, such as denied access ...

 import errno try: os.makedirs('C:\\test\\') except OSError as e: if e.errno != errno.EEXIST: raise # raises the error again 
+20
source share

can you try / besides?

 try: os.makedirs('C:\\test\\') except: pass 
-3
source share

All Articles