python 应用

一些小工具

多个文件重命名

1
2
3
4
5
import os
i=1
for file in os.listdir("."):
os.rename(file,"img"+str(i)+".bmp")
i=i+1

PS: windows 防火墙会阻止脚本文件更改文件名,所以要关闭

查找文档内容

1
2
3
4
5
6
7
import  os
target = input("please input the search word:")
for filename in os.listdir("."):
if "." in filename:
with open (filename,'r',encoding='UTF-8') as f:
if target in f.read():
print(os.path.basename(filename))

批量修改图片大小

1
2
3
4
5
from PIL import Image

img = Image.open("cat.bmp")
new_img = img.resize((40,40))
new_img.save("cat.bmp", "BMP", optimize=True)