码迷,mamicode.com
首页 > 编程语言 > 详细

python文件备份与简单操作

时间:2018-09-19 18:03:26      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:and   coding   etc   readlines   adl   备份   ntp   class   work   

 

#!/usr/bin/python
# -*- coding: utf-8 -*-
# data:2018/8/30
# user:fei

import sys
import random
num = random.sample(range(5,10),1)[0]
url1=‘server 10.10.4.yy‘ + ‘\n‘
url2=‘server 10.10.4.xx‘+ ‘\n‘
# url = url1 + url2
souse1 = ‘1xx‘
souse2 = ‘1yy‘
oldfile_url = ‘/etc/ntp.conf‘
newfile_url = ‘/home/work/opdir/ntp.conf-bak‘
src = file(oldfile_url,‘r‘)
dst = file(newfile_url,‘w‘)
dst.write(src.read())
src.close()
dst.close()
with open(newfile_url,"r") as f:

lines = f.readlines()

#写的方式打开文件
with open(oldfile_url,"w+") as f_w:
for line in lines:
if souse1 in line:
line = url1
elif souse2 in line:
line = url2
f_w.write(line)

  

 

python文件备份与简单操作

标签:and   coding   etc   readlines   adl   备份   ntp   class   work   

原文地址:https://www.cnblogs.com/tengfei520/p/9675805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!