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']


相关文章
|
4月前
|
Python
python如何使用os模块进行文件和目录操作?
python如何使用os模块进行文件和目录操作?
|
5月前
|
Python Windows
Python 扩展 快捷贴士:os模块下的创建目录的方式
如果子目录创建失败或者已经存在,会抛出一个 OSError 的异常,Windows上Error 183 即为目录已经存在的异常错误。
53 0
|
2月前
|
C语言
操作系统 | 编写内核模块
操作系统 | 编写内核模块
34 1
|
3天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
15 0
|
1月前
|
监控 API C语言
【Python 基础教程 22】全面揭秘Python3 os模块:从入门到高级的实用教程指南
【Python 基础教程 22】全面揭秘Python3 os模块:从入门到高级的实用教程指南
62 1
|
1月前
|
监控 数据挖掘 API
解锁Python库中操作系统级别模块psutil
解锁Python库中操作系统级别模块psutil
25 0
|
5月前
|
存储
【OS Pintos】用户程序是如何工作的 | Pintos 运行原理 | 虚拟内存 | 页函数 | 系统调用
【OS Pintos】用户程序是如何工作的 | Pintos 运行原理 | 虚拟内存 | 页函数 | 系统调用
164 0
|
8月前
python-- os 模块
python-- os 模块
|
8月前
|
Shell Python Windows
|
6月前
|
Shell Linux Go
《Linux操作系统编程》第八章 Shell程序设计: shell 语言结构,包括测试、分支、循环、跳转、函数、语句组
《Linux操作系统编程》第八章 Shell程序设计: shell 语言结构,包括测试、分支、循环、跳转、函数、语句组
67 0