Python os.makedirs() - 递归创建目录

Python os.makedirs()

os模块中的所有函数在文件名和路径无效或不可访问,或其他参数类型正确但操作系统不接受的情况下都会引发OSError。

Python os.makedirs() 方法递归创建目录。这意味着在创建叶目录时,如果任何中级目录缺失, os.makedirs() 方法将全部创建它们。

例如,考虑以下路径:

/home/User/Documents/GeeksForGeeks/Authors/ihritik

假设我们想创建目录“ihritik”,但是目录“GeeksForGeeks”和“Authors”在该路径中不可用。然后 os.makedirs() 方法将在指定路径中创建所有不可用/缺失的目录。’ GeeksForGeeks ‘和’ Authors ‘将首先创建,然后创建’ ihritik ‘目录。

语法: os.makedirs(path,mode= 0o777, exist_ok = False)

参数:

path:表示文件系统路径的类路径对象。类路径对象是表示路径的字符串或字节对象。

mode(可选):整数,表示新创建目录的模式。.如果省略该参数,则使用默认值Oo777。

exist_ok(可选):默认为False。如果目标目录已经存在,如果其值为False则引发OSError,否则不引发。如果值为True,则不改变目录。

返回类型: 此方法不返回任何值。

示例1

使用os.makedirs()方法创建目录

# Python program to explain os.makedirs() method
   
# importing os module
import os
 
# Leaf directory
directory = "ihritik"
 
# Parent Directories
parent_dir = "/home/User/Documents/GeeksForGeeks/Authors"
 
# Path
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'ihritik'
os.makedirs(path)
print("Directory '%s' created" %directory)
 
# Directory 'GeeksForGeeks' and 'Authors' will
# be created too
# if it does not exists
 
 
 
# Leaf directory
directory = "c"
 
# Parent Directories
parent_dir = "/home/User/Documents/GeeksforGeeks/a/b"
 
# mode
mode = 0o666
 
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'c'
  
os.makedirs(path, mode)
print("Directory '%s' created" %directory)
 
 
# 'GeeksForGeeks', 'a', and 'b'
# will also be created if
# it does not exists
 
# If any of the intermediate level
# directory is missing
# os.makedirs() method will
# create them
 
# os.makedirs() method can be
# used to create a directory tree 

输出:

Directory 'ihritik' created
Directory 'c' created

示例2

使用os.makedirs()方法时出错

# Python program to explain os.makedirs() method
   
# importing os module
import os
 
# os.makedirs() method will raise
# an OSError if the directory
# to be created already exists
 
    
# Directory
directory = "ihritik"
 
# Parent Directory path
parent_dir = "/home/User/Documents/GeeksForGeeks"
 
# Path
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'ihritik'
os.makedirs(path)
print("Directory '%s' created" %directory)

输出:

Traceback (most recent call last):
  File "makedirs.py", line 21, in 
    os.makedirs(path)
  File "/usr/lib/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)
FileExistsError: [Errno 17] File exists: '/home/User/Documents/GeeksForGeeks/ihritik'

示例3

使用os.makedirs()方法时的错误处理

# Python program to explain os.makedirs() method
   
# importing os module
import os
 
# os.makedirs() method will raise
# an OSError if the directory
# to be created already exists
# But It can be suppressed by
# setting the value of a parameter
# exist_ok as True
    
# Directory
directory = "ihritik"
 
# Parent Directory path
parent_dir = "/home/ihritik/Desktop/GeeksForGeeks"
 
# Path
path = os.path.join(parent_dir, directory)
 
# Create the directory
# 'ihritik'
try:
    os.makedirs(path, exist_ok = True)
    print("Directory '%s' created successfully" %directory)
except OSError as error:
    print("Directory '%s' can not be created")
 
 
# By setting exist_ok as True
# error caused due already
# existing directory can be suppressed
# but other OSError may be raised
# due to other error like
# invalid path name

输出:

Directory 'ihritik' created successfully

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程