try: os.makedirs("path/to/directory") except FileExistsError: # directory already exists pass
Here is what the above code is Doing:
1. It’s checking if the directory exists.
2. If it doesn’t exist, it creates it.
3. If it does exist, it does nothing.