# -*- coding: utf-8 -*-
import os
import re
# path=os.getcwd()
str_old = 'insert'
str_new = 'frs.event.queue'
file_formate = 'init.sql'
file_sql=open(r'F:\bak\init_all.sql', 'r+', encoding='utf-8')
def replace_txt(path):
if path.find(file_formate) == len(path) - len(file_formate):
with open(path, 'r+', encoding='utf-8') as file:
str = file.read()
if str.find(str_old) > 0:
print(path)
str = str.replace(str_old, str_new)
print(str)
file.seek(0, 0)
file.write(str)
# file.close()# replace_txt(r'D:\python\workspace\tools\util\txt12\test.txt.py')
def find_txt(path):
with open(path, 'r+', encoding='utf-8') as file:
i=0
while True:
line = file.readline()
i=i+1
if not line:
breakelse:
try:
str_line = str(line)
# if path.find(str_old) > 0:if str_line.find(str_old) >=0:
print(str_line)
break
except:
print(str(line))
2、如果不知道文件是否utf-8
def find_txt_no_formate(path):
if file_formate !='':
if path.find(file_formate) == len(path) - len(file_formate):
print(path)
with open(path, 'r+', encoding='utf-8') as file:
str = file.read()
file_sql.write('\n--------------------------------')
file_sql.write(path)
file_sql.write(str)
# find_txt(path)else:
find_txt(path)
3、列出所有文件
def list_all_file(rootdir):
files = []
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isdir(path):
files.extend(list_all_file(path))
if os.path.isfile(path):
files.append(path)
try:
find_txt_no_formate(path)
except:
print("e")
return files
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)