- 积分
- 36565
- 明经币
- 个
- 注册时间
- 2011-8-7
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2022-7-25 08:49:52
|
显示全部楼层
import os;
import os.path
def reset():
i = 0
j = 0
path = r"E:/Users/administrator/Desktop/MECpg/"
filelist = os.listdir(path) # 该文件夹下所有的文件(包括文件夹)
for files in filelist: # 遍历所有文件
i = i + 1
Olddir = os.path.join(path, files); # 原来的文件路径
if os.path.isdir(Olddir): # 如果是文件夹则跳过
continue;
#print(os.path.splitext(files))
#filename = os.path.splitext(files)[0]; # 文件名
#filetype = os.path.splitext(files)[1]; # 文件扩展名
filename,filetype=os.path.splitext(files)
filePath=path+filename+filetype
if filetype.lower() == ".lsp" :
if alter(filePath,"(command " , "(cmds " ):
j +=1
if j==0:
print("没有需要替换的内容")
def alter(file,old_str,new_str):
"""
替换文件中的字符串
:param file:文件名
:param old_str:旧字符串
:param new_str:新字符串
:return:
"""
file_data = ""
with open(file, "r", encoding="ANSI") as f:
goset = False
for line in f:
if old_str in line:
print(file)
print("替换前:",line)
line = line.replace(old_str,new_str)
print("替换后:",line)
goset = True
file_data += line
if goset:
print("文件重写中...")
with open(file,"w",encoding="ANSI") as f:
f.write(file_data)
return True
else: return False
if __name__=='__main__':
reset()
=========================
可以尝试用Python改,上面是我拼凑的程序,把所有lsp文件里的 "(command " 改成 "(cmds "
alter(filePath,"(command " , "(cmds " ):
|
|