Home  >  Article  >  Backend Development  >  How to create a directory structure that does not exist

How to create a directory structure that does not exist

anonymity
anonymityOriginal
2019-05-25 10:11:591756browse

In Python, OS libraries are often used to operate files. How to create a directory structure that does not exist

How to create a directory structure that does not exist

Determine whether the directory exists. If it does not exist, create a new one:

import os
if not os.path.isdir(dir_name):
    os.makedirs(dir_name)

os.mkdir() creates the last-level directory in the path. If the previous directory does not exist and needs to be created, an error will be reported.

os.makedirs() creates multi-level directories. If the intermediate directories do not exist, they will be created automatically.

The above is the detailed content of How to create a directory structure that does not exist. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn