1.要求:欲把一些图片按统一标准重命名

2.任务拆解:1)遍历文件夹下的文件

                 2)重命名

                 3)字符串操作

# -*- coding: utf-8 -*-
'''
Created on 2013-11-13 09:56
@summary:  Traverse folder and rename
@author: leaf
'''

import os
import os.path
rootdir = r"e:\picture"                                   # 指明被遍历的文件夹

for parent,dirnames,filenames in os.walk(rootdir):     #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
    for filename in filenames:                        #文件名
        os.rename(os.path.join(parent,filename),os.path.join(parent,filename[:-4]+'.black.png')) #重命名


用到知识点:

1.os.walk(dir):遍历指定目录

2.os.renme(old, new):文件重命名

3.os.path.join(a, b):连接组合路径

 

相关文章:

  • 2021-07-13
  • 2022-12-23
  • 2021-09-10
  • 2022-12-23
  • 2021-05-27
  • 2021-09-13
  • 2021-07-01
  • 2021-10-26
猜你喜欢
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
相关资源
相似解决方案