python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python  os与os.path使用

Python开发之os与os.path的使用小结

作者:思诺学长

这篇文章主要介绍了Python开发之os与os.path的使用小结,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧

1. os的一般用法

使用dir()列出库的属性与方法

# 使用dir()列出库的属性与方法
print(dir(os))

使用os.getcwd()打印当前目录

# 使用os.getcwd()打印当前目录
print("当前目录为:"+os.getcwd())  # 打印表示当前工作目录的字符串

获取指定路径下的目录和文件列表

# 获取指定路径下的目录和文件列表
root = r'/path/to/your/directory'  # 绝对路径
path = os.listdir(root)
print(path)

创建目录

# 创建目录
os.makedirs(r'B\C', exist_ok=True)  # 方法1.利用makedirs()创建多级目录
os.mkdir(r'A')  # 方法2.利用mkdir()创建单级目录

以当前目录作为相对目录的基准,在相对目录下的A中创建message.txt

# 以当前目录作为相对目录的基准,在相对目录下的A中创建message.txt
with open(r"A\message.txt", "w")as file:
    pass

删除空目录

# 删除空目录
os.removedirs(r"B\C")  # 使用removedirs()递归删除目录。
# os.rmdir(r"B\C")  # 删除目录

删除文件

# 删除文件
os.remove(r'A\message.txt')

完全删除一个目录以及所有内容

# 完全删除一个目录以及所有内容
import shutil
dir_path = "/path/to/your/directory"  # 将此处的路径替换为你要删除的目录路径
try:
    shutil.rmtree(dir_path)
    print("目录已成功删除。")
except OSError as e:
    print("删除目录时出错:", e)

2. os.path的用法

使用os.path.abspath() 打印"A\message.txt"的绝对路径

# 使用os.path.abspath()打印"A\message.txt"的绝对路径
print("message.txt的绝对路径为:"+os.path.abspath(r"A\message.txt"))  # "A\message.txt"换为你的相对路径下的路径
print("运行文件的绝对路径为:"+os.path.abspath(__file__))  # 当前路径

os.path.exists(path) 判断该路径或文件是否存在

# os.path.exists(path) 判断该路径或文件是否存在
print(os.path.exists(r'/path/to/your/directory'))
# 检查A\message.txt文件是否存在
print(os.path.exists(r'A\message.txt'))

os.path.dirname() 方法用于从指定路径中获取目录名,返回上一级的目录

#  os.path.dirname() 方法用于从指定路径中获取目录名,返回上一级的目录
# Path
path = r'\path\to\your\directory\A'
# 获取指定路径下的目录名
dirname = os.path.dirname(path)
# 打印目录名称
print(dirname)
# Path
path = r'\path\to\your\directory\A\message.txt'
# 获取指定路径下的目录名
dirname = os.path.dirname(path)
# 打印目录名称
print(dirname)

os.path.join() 拼接路径

# os.path.join()拼接路径
Path1 = 'home'
Path2 = 'develop'
Path3 = ''
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1, Path2, Path3)
Path30 = os.path.join(Path2, Path3, Path1)
print('Path10 = ', Path10)
print('Path20 = ', Path20)
print('Path30 = ', Path30)

3. 完整代码

如下:

# -*- coding: utf-8 -*-
# @Time : 23/05/2024 09:47
# @Author : jin
# @File : ex1.py
# @Project : python exercises
# @Descriptioon :
# @path : \Users\ jin\add_yourpath\ python exercises
import os
# os的一般用法
# 使用dir()列出库的属性与方法
print(dir(os))
# 使用os.getcwd()打印当前目录
print("当前目录为:"+os.getcwd())  # 打印表示当前工作目录的字符串
# 获取指定路径下的目录和文件列表
root = r'/path/to/your/directory'  # 绝对路径
path = os.listdir(root)
print(path)
# 创建目录
os.makedirs(r'B\C', exist_ok=True)  # 方法1.利用makedirs()创建多级目录
os.mkdir(r'A')  # 方法2.利用mkdir()创建单级目录
# 以当前目录作为相对目录的基准,在相对目录下的A中创建message.txt
with open(r"A\message.txt", "w")as file:
    pass
# 删除空目录
os.removedirs(r"B\C")  # 使用removedirs()递归删除目录。
# os.rmdir(r"B\C")  # 删除目录
# 删除文件
os.remove(r'A\message.txt')
# 完全删除一个目录以及所有内容
# import shutil
#
# dir_path = "/path/to/your/directory"  # 将此处的路径替换为你要删除的目录路径
#
# try:
#     shutil.rmtree(dir_path)
#     print("目录已成功删除。")
# except OSError as e:
#     print("删除目录时出错:", e)
# 以当前目录作为相对目录的基准,在相对目录下的A中创建message.txt
with open(r"A\message.txt", "w"):
    pass
"""-----------------------------------------------------------------------"""
# os.path的用法
# 使用os.path.abspath()打印"A\message.txt"的绝对路径
print("message.txt的绝对路径为:"+os.path.abspath(r"A\message.txt"))  # "A\message.txt"换为你的相对路径下的路径
print("运行文件的绝对路径为:"+os.path.abspath(__file__))  # 当前路径
# os.path.exists(path) 判断该路径或文件是否存在
print(os.path.exists(r'/path/to/your/directory'))
# 检查A\message.txt文件是否存在
print(os.path.exists(r'A\message.txt'))
#  os.path.dirname() 方法用于从指定路径中获取目录名
# Path
path = r'\path\to\your\directory\A'
# Get the directory name from the specified path
dirname = os.path.dirname(path)
# Print the directory name
print(dirname)
# Path
path = r'\path\to\your\directory\A\message.txt'
# Get the directory name from the specified path
dirname = os.path.dirname(path)
# Print the directory name
print(dirname)
# os.path.join()拼接路径
Path1 = 'home'
Path2 = 'develop'
Path3 = ''
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1, Path2, Path3)
Path30 = os.path.join(Path2, Path3, Path1)
print('Path10 = ', Path10)
print('Path20 = ', Path20)
print('Path30 = ', Path30)

到此这篇关于Python开发之os与os.path的使用小结的文章就介绍到这了,更多相关python os与os.path的使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文