python编写开发更新数据后台脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python编写开发更新数据后台脚本相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
#coding:utf-8
import os
import re
import shutil as copy
copy_web_service = ‘/data/web/s1‘
dest_web_service = ‘/data/web/s‘
all_web_service = ‘/data/web/‘
crontab_file = ‘/data/update/crontab.txt‘
openservice_num = ‘/data/update/open_num.txt‘
#获取开服数字并拷贝一个新目录出来
def copy_newservice():
global open_num
all_service = set()
alredy_open_service = set()
if not os.path.exists(openservice_num):
print(‘%s开服数文件不存在,请检查‘ % openservice_num)
exit(1)
with open(‘/data/update/open_num.txt‘,‘r‘) as f:
for file in f.readlines():
num = file.strip()
for nums in range(1,int(num)+1):
all_service.add(nums)
for service in os.listdir(all_web_service):
if re.match(‘sd+‘,service):
alredy_open_service.add(int(service[1:]))
cha_num = alredy_open_service^all_service
open_num = list(cha_num)
if not os.path.exists(copy_web_service):
print(‘%s目录不存在,脚本退出‘ % copy_web_service)
exit(1)
else:
for nums in open_num:
dest_dir = dest_web_service + str(nums)
if not os.path.exists(dest_dir):
copy.copytree(copy_web_service,dest_dir)
#修改a.py脚本区服名字三处
def modify_script():
script = ‘‘
modify_list = [‘s1‘,‘s1‘,‘s1‘]
for nums in open_num:
new = ‘s‘ + str(nums)
for name in modify_list:
dest_dir = dest_web_service + str(nums)
if not os.path.exists(dest_dir+‘/a.py‘):
print(‘%s脚本不存在,脚本退出‘ % (dest_dir+‘/a.py‘))
exit(1)
with open(dest_dir+‘/a.py‘) as f:
for line in f.readlines():
if line.find(str(name)):
line = re.sub(name,new,line)
script += line
else:
script += line
with open(dest_dir+‘/a.py‘,‘w‘) as f:
f.write(script)
script = ‘‘
#清空do.txt和insert.log文件
def clear_file():
kong = ‘‘
file_list = [‘/insert.log‘,‘/do.txt‘]
for nums in open_num:
for files in file_list:
dest_dir = dest_web_service + str(nums)
if not os.path.exists(dest_dir+files):
print(‘%s文件不存在,脚本退出‘ % (dest_dir+files))
else:
with open(dest_dir+files,‘w‘) as f:
f.write(kong)
#创建定时任务
def crontab():
if not os.path.exists(crontab_file):
print(‘%s文件不存在,请检查‘ % crontab_file)
for nums in open_num:
task = ‘*/5 * * * * python /data/web/s‘ + str(nums) + ‘/a.py >> /data/web/s‘ + str(nums) + ‘/insert.log
‘
with open(crontab_file,‘a‘) as f:
f.write(task)
os.system(‘crontab %s‘ % crontab_file)
copy_newservice()
modify_script()
clear_file()
crontab()
以上是关于python编写开发更新数据后台脚本的主要内容,如果未能解决你的问题,请参考以下文章