搜索
首页后端开发Python教程python下os模块强大的重命名方法renames详细介绍

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]: [&#39;test3&#39;]

    同样的,他可以修改目录名,不知道能否连文件名,目录名一起修改呢,下面我们再来看下效果:

In [22]: os.listdir("/tmp/test1/test3/")
Out[22]: [&#39;test&#39;]       新建一个空文件
 
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: &#39;/tmp/test/test2/test3&#39;
 
In [26]: os.listdir("/tmp/test/test2/")  浏览效果
Out[26]: [&#39;test3&#39;]
 
In [27]:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多python下os模块强大的重命名方法renames详细介绍相关文章请关注PHP中文网!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

对于循环和python中的循环时:每个循环的优点是什么?对于循环和python中的循环时:每个循环的优点是什么?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)