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

关注公众号:【小张Python】,为你准备了 50+ 本Python 精品电子书籍 与 50G + 优质视频学习资料,后台回复关键字:1024 即可获取;如果对博文内容有什么疑问,后台添加作者【个人微信】,可与作者直接进行交流

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
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员大飞1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值