python anytree_Python os.makedirs()用法及代码示例

Python中的OS模块提供了与操作系统进行交互的功能。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的功能的便携式方法。

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

os.makedirs()Python中的方法用于递归创建目录。这意味着在创建叶子目录时,如果缺少任何intermediate-level目录,os.makedirs()方法将全部创建。

例如,考虑以下路径:

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

假设我们要创建目录“ ihritik”,但目录“ GeeksForGeeks”和“作者”在路径中不可用。然后os.makedirs()方法将在指定路径中创建所有不可用/缺少的目录。首先创建“ GeeksForGeeks”和“作者”,然后创建“ ihritik”目录。

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

参数:

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

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

exist_ok(可选):此参数使用默认值False。如果目标目录已经存在,则如果其值为False,则会引发OSError,否则将引发OSError。

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

代码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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值