码迷,mamicode.com
首页 > 其他好文 > 详细

批量修改文件名字

时间:2019-11-07 15:04:07      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:批量修改   itext   strong   new   txt   spl   获取   getc   listdir   

修改任意目录下文件名字

path_dir = r'目录地址'
# 获取目录下的所有文件
a = os.listdir(path_dir)
n = 0
for i in a:
    n += 1
    x = str(n)
    # 分割路径,返回路径名和文件扩展名的元组
    ext = os.path.splitext(i)
    # 只修改后缀为.txt的文件
    if ext[1] == '.txt'
        # 修改后的名字
        new_name = x + ext[1]
        # 要修改的路径下的文件
        oldfile = os.path.join(path_dir, i)
        # 修改后名字的路径
        newfile = os.path.join(path_dir, new_name)
        # 重命名
        os.rename(oldfile, newfile)

修改当前目录下文件名字

# 当前目录
path_dir = os.getcwd()
# 获取目录下的所有文件
a = os.listdir()
n = 0
for i in a:
    n += 1
    x = str(n)
    # 分割路径,返回路径名和文件扩展名的元组
    ext = os.path.splitext(i)
    # 只修改后缀为.txt的文件
    if ext[1] == '.txt':
        # 修改后的名字
        new_name = x + ext[1]
        # 要修改的路径下的文件
        oldfile = os.path.join(path_dir, i)
        # 修改后名字的路径
        newfile = os.path.join(path_dir, new_name)
        # 重命名
        os.rename(oldfile, newfile)

批量修改文件名字

标签:批量修改   itext   strong   new   txt   spl   获取   getc   listdir   

原文地址:https://www.cnblogs.com/jie9527-/p/11811914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!