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详细介绍相关文章请关注PHP中文网!

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

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

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

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

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

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

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

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


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

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

Dreamweaver CS6
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)