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

How to create a directory structure that does not exist

anonymity
Release: 2019-05-25 10:12:08
Original
1802 people have browsed it

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template