python下os模块强大的重命名方法renames详解
作者:lqh 时间:2021-05-12 17:19:34
python下os模块强大的重命名方法renames详解
在python中有很多强大的模块,其中我们经常要使用的就是OS模块,OS模块提供了超过200个方法来供我们使用,并且这些方法都是和数据处理相关的,这里介绍下重命名这个方法。
OS的重命名方法是os.rename,我用的ipython,这个玩意很是强大,只要按下TAB键,可以帮助我们自动对齐和列出可以使用的方法,发现有2个方法,分别是rename和renames,2个方法,前面的rename使用过无数次,但是后面的renames还没有使用过,今天有空,想看看到底有和用处---顾名思义,我觉得可能是批量修改吧,哈哈,下面我们来看下演示效果:
[root@localhost ~]# ipython 进入ipython工具
Python 2.6 (r26:66714, Dec 17 2010, 11:17:00)
Type "copyright", "credits" or "license" for more information.
IPython 0.10.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: import os 导入OS模块
In [2]: os.mkdir("/tmp/test") 新建目录
In [3]: os.listdir("/tmp/test")
Out[3]: []
In [4]: os.mkdir("/tmp/test/test1") 新建目录
In [5]: os.listdir("/tmp/test/")
Out[5]: ['test1']
In [6]: os.listdir("/tmp/test/test1")
Out[6]: []
In [7]: os.ren 按Tab键自动对齐的效果
os.rename os.renames
In [7]: os.rename("/tmp/test/test1","/tmp/test/test2")
重命名目录名
In [8]: os.listdir("/tmp/test/") 修改成功
Out[8]: ['test2']
In [9]: os.rename 按Tab键自动对齐的效果
os.rename os.renames
In [9]: os.rename("/tmp/test/test2","/tmp/test1/test3")
大家注意看下这个命令和上面的命令的区别,我不仅仅是要重命名最后的目录名,连前面的目录也要重命名
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
/root/<ipython console> in <module>()
OSError: [Errno 2] No such file or directory
嘿嘿,报错了,说没有这个目录,
下面在看下下面这个强大的方法吧,哈哈
In [10]: os.renames("/tmp/test/test2","/tmp/test1/test3")
In [11]: os.listdir("/tmp/test1/test3")
Out[11]: []
嘿嘿,居然成功了,真是强大啊,子目录能改,上级目录也能改,这个法子也许对我们工作会有帮助吧。
In [12]: os.listdir("/tmp/test1")
Out[12]: ['test3']
同样的,他可以修改目录名,不知道能否连文件名,目录名一起修改呢,下面我们再来看下效果:
In [22]: os.listdir("/tmp/test1/test3/")
Out[22]: ['test'] 新建一个空文件
In [23]: cat /tmp/test1/test3/test
In [24]: os.renam
os.rename os.renames
In [24]:
os.renames("/tmp/test1/test3/test","/tmp/test/test2/test3")
批量修改文件名和目录名,嘿嘿,果然也成功了。
In [25]: os.listdir("/tmp/test/test2/test3")
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
/root/<ipython console> in <module>()
OSError: [Errno 20] Not a directory: '/tmp/test/test2/test3'
In [26]: os.listdir("/tmp/test/test2/") 浏览效果
Out[26]: ['test3']
In [27]:
希望本文能对大家的工作和学习有帮助,如果感兴趣,可以自己去试验下。。。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:python,os模块,renames
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于JS判断iframe是否加载成功的方法(多种浏览器)
2023-08-24 04:14:52
SQL Server索引管理的六大铁律
2009-03-25 14:05:00
python中字典按键或键值排序的实现代码
2023-07-22 00:26:49
Pytest中skip skipif跳过用例详解
2022-07-21 09:42:30
![](https://img.aspxhome.com/file/2023/0/87530_0s.png)
MySQL 5数据库连接超时问题
2009-12-29 10:23:00
The Story of Mr.Gray — Web 交互设计“灰色”的8类应用
2009-12-30 16:57:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/mr-gray-01-93s.jpg)
获得MySQL改变字符集的方案
2010-08-31 14:53:00
Python readline()和readlines()函数实现按行读取文件
2022-02-21 13:19:36
如何基于Python代码实现高精度免费OCR工具
2022-10-17 07:53:47
解析:MySQL对“服务器端光标”的限制
2008-11-27 16:22:00
python中opencv 直方图处理
2021-12-24 09:45:17
![](https://img.aspxhome.com/file/2023/5/90595_0s.webp)
Python的爬虫程序编写框架Scrapy入门学习教程
2021-12-27 10:01:48
![](https://img.aspxhome.com/file/2023/5/76705_0s.png)
HTML的基本元素
2010-03-16 12:39:00
JavaScript控制flash操作 兼容IE FF[译]
2009-11-29 16:28:00
![](https://img.aspxhome.com/file/UploadPic/200911/29/flashjr-60s.jpg)
用python实现一个简单计算器(完整DEMO)
2023-11-10 11:03:48
![](https://img.aspxhome.com/file/2023/0/67240_0s.png)
浅谈Python响应式类库RxPy
2021-12-24 12:44:26
![](https://img.aspxhome.com/file/2023/3/78453_0s.png)
python随机模块random的22种函数(小结)
2022-08-11 18:09:03
![](https://img.aspxhome.com/file/2023/5/76715_0s.png)
互联网产品的用户体验看着“很美”
2009-07-07 12:04:00
![](https://img.aspxhome.com/file/UploadPic/20097/7/12031-10s.jpg)
如何用Python对数学函数进行求值、求偏导
2023-10-13 04:58:01
Python StringIO如何在内存中读写str
2022-08-01 13:45:31