os模块下 getcwd()、chidr()、mkdir()、makedirs()、listdir() 等函数用法介绍

简介: os.getcwd()cwd 全称为 Current Working Directory(CWD) ,即为当前工作路径;os.getcwd() 函数返回的就是当前工作路径

os.getcwd()

cwd 全称为 Current Working Directory(CWD) ,即为当前工作路径;os.getcwd() 函数返回的就是当前工作路径


cwd 的作用

Python 调用脚本时需要指定脚本名称,调用时解释器首先会从当前工作路径下进行搜索,如果文件名位于当前文件夹下或配置到环境变量中,则调用成功否则调用失败

>>> import os
>>> os.getcwd()
'D:\\Data\\map_data'

os.chidr(path)

  • path(str),文件夹目录;

改变 当前工作路径path

>>> os.getcwd()
'D:\\Data\\map_data'
>>> os.chdir('../')#返回上一个目录
>>> os.getcwd()
'D:\\Data'

os.mkdir(path)

  • path(str),文件夹目录

创建一个文件夹目录,如果 path 存在则程序报错

>>> os.listdir(os.getcwd())
['map-location.xlsx', 'Map2.gif', 'Map22.gif']
>>> os.mkdir(os.getcwd() + '/make_dir')
>>> os.listdir(os.getcwd())
['make_dir', 'map-location.xlsx', 'Map2.gif', 'Map22.gif']
>>> os.mkdir(os.getcwd() +'/make_dir')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'D:\\Data\\map_data/make_dir'

os.makedirs(path)

  • path(str),文件夹目录


递归创建文件夹目录 path,若中间部分文件夹不存在时则自动创建;例如假设我想创建一个 D:/Data/Ceshi/Ceshi1 目录文件夹,而系统中没有 D:/Data/Ceshi 文件夹,则在调用 os.makedirs(path) 函数创建前者时,后者自动创建;

>>> os.listdir(os.getcwd())
['make_dir', 'map-location.xlsx', 'Map2.gif', 'Map22.gif']
>>> os.makedirs(os.getcwd() + '/recu_dir/file_dir')
>>> os.listdir(os.getcwd())
['make_dir', 'map-location.xlsx', 'Map2.gif', 'Map22.gif', 'recu_dir']
>>> os.listdir(os.getcwd() + '/recu_dir')
['file_dir']

os.listdir(path)

  • path(str),一个文件路径;

返回一个列表,列出在目录 path 下的文件目录和文件名(没有加入递归操作);未指定 path 则默认为 当前工作路径

>>> os.listdir()
['make_dir', 'map-location.xlsx', 'Map2.gif', 'Map22.gif', 'recu_dir']
>>> os.listdir(os.getcwd())
['make_dir', 'map-location.xlsx', 'Map2.gif', 'Map22.gif', 'recu_dir']

os.remove(path)

  • path(str),表示一个文件路径;

删除一个文件路径,**注意这个函数不能删除文件夹,如果 path 指定的是文件夹的话会报错 **

>>> os.remove(os.getcwd() +'/make_dir')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [WinError 5] 拒绝访问。: 'D:\\Data\\map_data/make_dir'
>>> os.remove(os.getcwd() +'/Map21.gif')
>>> os.listdir()
['make_dir', 'map-location.xlsx', 'Map2.gif', 'Map22.gif', 'recu_dir']

os.rmdir(path)

  • path(str),表示文件夹目录

删除一个文件夹 path ,需要注意的是 文件夹 path 必须为空的,否则报错

>>> os.listdir()
['make_dir', 'map-location.xlsx', 'Map2.gif', 'Map22.gif', 'recu_dir']
>>> os.rmdir(os.getcwd() + '/recu_dir')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [WinError 145] 目录不是空的。: 'D:\\Data\\map_data/recu_dir'
>>> os.rmdir(os.getcwd() +'/make_dir')
>>> os.listdir()
['map-location.xlsx', 'Map2.gif', 'Map22.gif', 'recu_dir']


os.rename(path1,path2)

  • path1(str),未修改之前的文件路径,或文件夹目录;
  • path2(str),准备修改的文件路径;



将原文件 path1 重命名为 path2 ,前提需要保证源文件 path1 存在并且用户有足够权限修改它

>>> os.listdir()
['map-location.xlsx', 'Map22.gif', 'recu_dir']
>>> os.rename(os.getcwd() +'/Map22.gif',os.getcwd() +'/Map123.gif')
>>> os.listdir()
['map-location.xlsx', 'Map123.gif', 'recu_dir']


相关文章
|
6天前
|
存储 JavaScript 前端开发
nodejs os模块
nodejs os模块
15 0
|
2月前
|
Python
python--os模块
python--os模块
22 2
|
2月前
|
Python
Python的`os`模块核心功能概述:通过`os.getcwd()`获取
【6月更文挑战第23天】Python的`os`模块核心功能概述:通过`os.getcwd()`获取、`os.chdir()`改变工作目录;使用`os.mkdir()`, `os.makedirs()`创建目录,`os.rmdir()`, `os.removedirs()`删除;`os.rename()`, `os.renames()`重命名文件或目录;`os.remove()`删除文件;`os.listdir()`列出目录内容;`os.path.exists()`, `os.path.isfile()`, `os.path.isdir()`检查路径;`os.stat()`获取文件属性。
35 4
|
1月前
|
Python
在Python中,`os`模块提供了与操作系统交互的多种方式。
在Python中,`os`模块提供了与操作系统交互的多种方式。
|
1月前
|
Python
Python的`signal`模块提供了访问底层操作系统提供的信号机制的方式。信号是操作系统用来通知进程发生了某种情况(如用户按下Ctrl+C)的一种机制。
Python的`signal`模块提供了访问底层操作系统提供的信号机制的方式。信号是操作系统用来通知进程发生了某种情况(如用户按下Ctrl+C)的一种机制。
|
2月前
|
Unix Shell Python
Python教程:开箱即用的Python os模块知识
注:文末有福利小工具源码 Python 的 os 模块提供了许多接口用于与操作系统进行交互,包括文件操作、目录操作、路径操作、环境变量操作、系统信息获取等
27 1
|
3月前
|
Linux
探索Linux操作系统的内核模块
本文将深入探讨Linux操作系统的核心组成部分——内核模块,揭示其背后的工作机制和实现方式。我们将从内核模块的定义开始,逐步解析其加载、卸载以及与操作系统其他部分的交互过程,最后探讨内核模块在系统性能优化中的关键作用。
|
1月前
|
安全 Linux 网络安全
部署07--远程连接Linux系统,利用FinalShell可以远程连接到我们的操作系统上
部署07--远程连接Linux系统,利用FinalShell可以远程连接到我们的操作系统上
|
1月前
|
Linux 虚拟化 数据安全/隐私保护
部署05-VMwareWorkstation中安装CentOS7 Linux操作系统, VMware部署CentOS系统第一步,下载Linux系统,/不要忘, CentOS -7-x86_64-DVD
部署05-VMwareWorkstation中安装CentOS7 Linux操作系统, VMware部署CentOS系统第一步,下载Linux系统,/不要忘, CentOS -7-x86_64-DVD
|
6天前
|
编解码 安全 Linux
基于arm64架构国产操作系统|Linux下的RTMP|RTSP低延时直播播放器开发探究
这段内容讲述了国产操作系统背景下,大牛直播SDK针对国产操作系统与Linux平台发布的RTMP/RTSP直播播放SDK。此SDK支持arm64架构,基于X协议输出视频,采用PulseAudio和Alsa Lib处理音频,具备实时静音、快照、缓冲时间设定等功能,并支持H.265编码格式。此外,提供了示例代码展示如何实现多实例播放器的创建与管理,包括窗口布局调整、事件监听、视频分辨率变化和实时快照回调等关键功能。这一技术实现有助于提高直播服务的稳定性和响应速度,适应国产操作系统在各行业中的应用需求。