2021年10月26日 14:38 阅读量:560
背景:下载网页包含图片和样式文件,需要批量重命名文件名称
python实现批量替换".下载",最好linux下执行,windows有乱码问题
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
# 输入文件夹地址
path = r"C:\Users\xxxx\Desktop\tmp"
files = os.listdir(path)
# 输出所有文件名,只是为了看一下
for file in files:
print(file)
# 获取旧名和新名
i = 0
for file in files:
# old 旧名称的信息
old = path + os.sep + files[i]
# new是新名称的信息,这里的操作是删除了里面面的'.下载 '
new = path + os.sep + file.replace(".下载","")
# 新旧替换
os.rename(old,new)
i+=1
shell实现批量去掉".png"文件名中的空格
#!/bin/bash
for file in `ls | grep .png`
do
newfile=`echo $file | sed 's/[[:space:]]//g`
mv $file $newfile
done