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")