大写字母 - 它们有什么意义?他们给你的只是rsi.
我想从我的目录结构中删除尽可能多的大写.我将如何编写一个脚本来在python中执行此操作?
它应递归地解析指定的目录,用大写字母标识文件/文件夹名称,并用小写重命名.
Joc*_*zel 14
os.walk 很适合用文件系统做递归的东西.
import os
def lowercase_rename( dir ):
# renames all subforders of dir, not including dir itself
def rename_all( root, items):
for name in items:
try:
os.rename( os.path.join(root, name),
os.path.join(root, name.lower()))
except OSError:
pass # can't rename it, so what
# starts from the bottom so paths further up remain valid after renaming
for root, dirs, files in os.walk( dir, topdown=False ):
rename_all( root, dirs )
rename_all( root, files)
Run Code Online (Sandbox Code Playgroud)
向上走树的意义在于,当你有一个像'/ A/B'这样的目录结构时,你也会在递归过程中得到路径'/ A'.现在,如果从顶部开始,则首先将/ A重命名为/ a,从而使/ A/B路径无效.另一方面,当您从底部开始并首先将/ A/B重命名为/ A/B时,它不会影响任何其他路径.
实际上你也可以使用os.walk自上而下,但那(稍微)更复杂.