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

来源:http://www.shanhubei.com/archives/2622.html

接手老项目,里面的文档大小心不同意,而在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)

你可能感兴趣的:(linux,服务器,python)