python把指定文件夹内所有文件和子文件夹大写全部改为小写

接手老项目,里面的文档大小心不同意,而在linux中对大小写又敏感。所以那就统一下把所有文件和文件夹全部转为小写

#!/usr/bin/python

import sys, os

total = 0


def rename(directory):
  global total
  if directory != directory.lower():
    os.rename(directory, directory.lower())
    # time.sleep(1)
    # total += 1
    print(directory)
  directory = directory.lower()

  # rename children
  for fn in os.listdir(directory):
    path = os.path.join(directory, fn)
    if path != path.lower():
      os.rename(path, path.lower())
      print(path)
      total += 1
    path = path.lower()

    # rename children within
    if os.path.isdir(path):
      rename(path)

rename_dir('/xxx/appimage')

print(total)
微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?