1.批量删除文件夹下每个文件中相邻的字符:
#!/usr/bin/python
import os
path = "./DATA"
files = os.listdir(path)
for file in files:
print(file)
f = open(path + "/" + file, "r")
f_out = open(path + "/" + file + ".bak", "w")
f.seek(1, 1)
while True:
str = f.read(1)
if not str:
break
print(str)
f_out.write(str)
f.seek(1, 1)
f.close()
f_out.close()
path1 = "./DATA"
files1 = os.listdir(path1)
for file1 in files1:
print(file1)
print(file1.find(".bak"))
if 0 < file1.find(".bak"):
os.rename(path1 + "/" + file1, path1 + "/" + file1 + ".koson")
注释:1.首先读取文件夹下某个文件的字符串,跳过相邻的字符保存到另一个文件中,然后遍历文件夹下的所有文件进行处理;2.再遍历文件夹下所有新建的文件进行重命名;