Python| os.makedirs() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
os 模块中的所有函数在文件名和路径无效或不可访问的情况下,或具有正确类型但操作系统不接受的其他参数的情况下引发OSError 。
Python中的os.makedirs()方法用于递归创建目录。这意味着如果缺少任何中间级目录,则在创建叶目录时, os.makedirs()方法将全部创建它们。
例如考虑以下路径:
/home/User/Documents/GeeksForGeeks/Authors/ihritik
假设我们要创建目录 'ihritik' 但目录 'GeeksForGeeks' 和 'Authors' 在路径中不可用。然后os.makedirs()方法将在指定路径中创建所有不可用/缺失的目录。将首先创建“GeeksForGeeks”和“Authors”,然后创建“ihritik”目录。
Syntax: os.makedirs(path, mode = 0o777, exist_ok = False)
Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
mode (optional) : A Integer value representing mode of the newly created directory..If this parameter is omitted then the default value Oo777 is used.
exist_ok (optional) : A default value False is used for this parameter. If the target directory already exists an OSError is raised if its value is False otherwise not. For value True leaves directory unaltered.
Return Type: This method does not return any value.
代码 #1:使用 os.makedirs() 方法创建目录
Python3
# 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
Python3
# 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)
Python3
# 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
Directory 'c' created
代码 #2:使用 os.makedirs() 方法时出错
Python3
# 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() 方法处理错误
Python3
# 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
参考: https://docs。 Python.org/3/library/os.html