Skip to content

Instantly share code, notes, and snippets.

@Hosuke
Created July 20, 2023 07:18
Show Gist options
  • Save Hosuke/f8360eaaa565cc4041ca40823f5fd187 to your computer and use it in GitHub Desktop.
Save Hosuke/f8360eaaa565cc4041ca40823f5fd187 to your computer and use it in GitHub Desktop.
import os
import re
# 获取当前工作目录
directory = os.getcwd()
# 遍历目录及其所有子目录下的所有文件
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
# 只处理.sql文件
if filename.endswith('.sql'):
filepath = os.path.join(dirpath, filename)
# 读取文件内容
with open(filepath, 'r') as file:
filedata = file.read()
# 使用正则表达式替换日期,忽略大小写
filedata = re.sub(r"DATE\s+'([^']*)'", r"TIMESTAMP '\1'", filedata, flags=re.IGNORECASE)
# 将修改后的内容写回文件
with open(filepath, 'w') as file:
file.write(filedata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment