批量去除文件空格

import os

filepath = r"G:\picture"  # 文件目录名
allfilepath = os.listdir(filepath)

for file in allfilepath:  # 改目录下的文件名
    oldpath = filepath + '\\' + file
    newname = file.replace(' ', '') # 在原先文件名中去除空格,也就是用null替代空格
    newpath = filepath + '\\' + newname # 新命名文件路径保存不变
    os.rename(oldpath, newpath)
    print("{} is renamed".format(newname))

print("ok")

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41264055/article/details/126640098