import os
# 获取当前工作目录
current_directory = os.getcwd()
print(f"当前工作目录是: {current_directory}")
# 列出指定目录下的所有文件和文件夹
directory_path = "/path/to/directory"
files_and_directories = os.listdir(directory_path)
print(f"目录 {directory_path} 下的内容: {files_and_directories}")
# 创建一个新的目录
new_directory_path = "/path/to/new_directory"
os.mkdir(new_directory_path)
print(f"已创建新目录: {new_directory_path}")
# 删除一个目录
os.rmdir(new_directory_path)
print(f"已删除目录: {new_directory_path}")
# 获取环境变量
environment_variable = os.getenv("PATH")
print(f"环境变量 PATH 的值是: {environment_variable}")
# 执行系统命令
os.system("ls -l")
获取当前工作目录:
os.getcwd() 返回当前工作目录的路径。列出指定目录下的所有文件和文件夹:
os.listdir(path) 返回指定路径下的所有文件和文件夹的列表。创建一个新的目录:
os.mkdir(path) 在指定路径下创建一个新的目录。删除一个目录:
os.rmdir(path) 删除指定路径的空目录。获取环境变量:
os.getenv(key) 获取指定环境变量的值,如果不存在则返回 None。执行系统命令:
os.system(command) 执行指定的系统命令,并返回命令的退出状态。上一篇:python自动化脚本
下一篇:python读取json文件
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站