Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python os库

作者:朕不想活了   发布日期:2025-07-13   浏览:91

import os

# 获取当前工作目录
current_directory = os.getcwd()
print(f"当前工作目录: {current_directory}")

# 列出指定目录下的所有文件和文件夹
directory_contents = os.listdir(current_directory)
print(f"目录内容: {directory_contents}")

# 创建一个新的文件夹
new_folder = "new_folder"
os.makedirs(new_folder, exist_ok=True)
print(f"创建文件夹: {new_folder}")

# 删除一个文件夹
os.rmdir(new_folder)
print(f"删除文件夹: {new_folder}")

# 获取文件的绝对路径
file_path = os.path.abspath(__file__)
print(f"文件的绝对路径: {file_path}")

# 检查路径是否存在
path_exists = os.path.exists(current_directory)
print(f"路径存在吗? {path_exists}")

# 检查是否为文件
is_file = os.path.isfile(file_path)
print(f"是文件吗? {is_file}")

# 检查是否为目录
is_directory = os.path.isdir(current_directory)
print(f"是目录吗? {is_directory}")

解释说明:

  1. os.getcwd():获取当前工作目录。
  2. os.listdir(path):列出指定路径下的所有文件和文件夹。
  3. os.makedirs(path, exist_ok=True):递归创建文件夹,如果文件夹已存在且 exist_ok=True,则不会抛出异常。
  4. os.rmdir(path):删除空文件夹。
  5. os.path.abspath(path):返回给定路径的绝对路径。
  6. os.path.exists(path):检查路径是否存在。
  7. os.path.isfile(path):检查路径是否为文件。
  8. os.path.isdir(path):检查路径是否为目录。

这些函数可以帮助你进行基本的文件和目录操作。

上一篇:python url编码

下一篇:python逻辑运算符

大家都在看

python时间格式

python开发windows应用程序

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

python的for i in range

npm config set python

python代码简单

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站