1. import os
    2. import sys
    3. name = 'jpg.jpg'
    4. path = '.'
    5. for root, dirs, files in os.walk(path): # path 为根目录
    6. for f in files:
    7. if name in f.lower():
    8. newname = f[0:-4]
    9. print(newname)
    10. os.rename(f, newname)
    11. for root, dirs, files in os.walk(path): # path 为根目录
    12. for f in files:
    13. if ' ' in f:
    14. print('有空格的',f)
    15. newname= f.replace(' ', '')
    16. os.rename(f, newname)
    17. for root, dirs, files in os.walk(path): # path 为根目录
    18. for f in files:
    19. if '-' in f:
    20. print('-连接线的:',f)
    21. newname= f.replace('-', '_')
    22. os.rename(f, newname)
    23. for root, dirs, files in os.walk(path): # path 为根目录
    24. for f in files:
    25. if '—' in f:
    26. print('—连接线的:',f)
    27. newname= f.replace('—', '_')
    28. os.rename(f, newname)
    29. for root, dirs, files in os.walk(path): # path 为根目录
    30. for f in files:
    31. if '_' in f:
    32. print('_大下划线的:',f)
    33. newname= f.replace('_', '_')
    34. os.rename(f, newname)
    35. for root, dirs, files in os.walk(path): # path 为根目录
    36. for f in files:
    37. if not '_' in f or f[10:11] != '_' or f[-6:-5] != '_':
    38. print('下划线位置不正确的:',f)
    39. for root, dirs, files in os.walk(path): # path 为根目录
    40. for f in files:
    41. if 'png' in f :
    42. print('是png格式的:',f)