<p>How do I check if the directory into which a file is going to be written does exist, and if it does not, create the directory using Python?</p>

Try the
```
os.path.exists
```
function
```
```
if not os.path.exists(dir):
    os.mkdir(dir)
```
```