根据导出的查询结果拼接字符串,生成sql语句并保存到txt文件中
import os os.chdir("C:/") path = os.getcwd() print(path) f = open("sql.csv") # print(f.read()) f.seek(0) lst = [] n = 0 for line in f.readlines(): if n > 0: fullname = line.rsplit('/', 1) #从右侧开始以第一个"/"为分隔符将字符串分割为两端,保存为2元素列表传给fullname filename = fullname[1].split('"') #第二个元素中还包含引号和回车,以引号为分隔符,提取出文件名 # print (filename[0]) str1 = "alter database rename file " str2 = " to '" str3 = "/u01/app/product/oradata/water/onlinelog/" str4 = "';" line1 = line.replace('\n', '') #删除每行的回车字符 sql = str1 + line1 + str2 + str3 + filename[0] + str4 #拼接成sql语句 #print(sql) lst.append(sql) n += 1 os.chdir("c:/Users/wuyi/Desktop") f_w = open('resultSql.txt','w') for i in range(len(lst)): lst[i] = lst[i] + '\n' f_w.writelines(lst) f.close() f_w.close()