python把文件从一个目录复制到另外一个目录,并且备份
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os,sys,md5,datetime,shutil,time,zipfile,chardet # coding=utf-8
#pub_path='/home/gdmt/mm'
pub_dir=''
src_dir='/home/gdmt/mm/ssss'
bak_dir='/home/gdmt/mm/bak'
bbdir='/home/gdmt/mm/bak/bbbb'
def pub(pub_file):
#遍历filepath下所有文件,包括子目录
files = os.listdir(pub_file)
for fi in files:
fi_d = os.path.join(pub_file,fi)
if os.path.isdir(fi_d):
is_exists=os.path.exists(fi_d.replace(pub_dir,src_dir))
if not is_exists:
shutil.copytree(fi_d,fi_d.replace(pub_dir,src_dir))
#print chardet.detect(fi_d)
#print fi_d.decode("gbk").encode("utf-8")
print 'publish dir:'+fi_d.replace(pub_dir,src_dir)
is_exists=os.path.exists(fi_d.replace(pub_dir,bak_dir))
if not is_exists:
os.mkdir(fi_d.replace(pub_dir,bak_dir))
pub(fi_d)
else:
#back
#print 'pub_dir:'+pub_dir
#print 'src_dir:'+src_dir
#print 'bak_dir:'+bak_dir
#print 'fi_d:'+fi_d
#print 'publish file:'+fi_d.replace(pub_dir,src_dir)
is_exists=os.path.exists(fi_d.replace(pub_dir,src_dir))
#is_exists2=os.path.exists(fi_d.replace(pub_dir,bak_dir))
#print is_exists
#print fi_d.replace(pub_dir,bak_dir)
if is_exists:
print 'bak file:'+fi_d.replace(pub_dir,bak_dir)
shutil.move(fi_d.replace(pub_dir,src_dir),fi_d.replace(pub_dir,bak_dir))
#publish
shutil.copyfile(fi_d,fi_d.replace(pub_dir,src_dir))
print 'publish file:'+fi_d.replace(pub_dir,src_dir) def un_zip(file_name,unzip_dir):
"""unzip zip file"""
zip_file = zipfile.ZipFile(file_name)
if os.path.isdir(unzip_dir):
pass
else:
os.mkdir(unzip_dir)
for names in zip_file.namelist():
#print chardet.detect(names)
#ss=names.decode('ascii')
#print ss
#ss=ss.encode('utf-8')
#print ss
zip_file.extract(names,unzip_dir)
zip_file.close() def cov_unicode(s):
return s.decode('ascii').encode('utf-8') file_name=sys.argv[1]
#zip_file='/home/gdmt/mm/pub/'+sys.argv[1]+'.zip'
pub_file='/home/gdmt/mm/pub/'+sys.argv[1]
pub_dir='/home/gdmt/mm/pub'
now = datetime.datetime.now()
t_str = now.strftime('%Y%m%d%H%M%S')
bak_dir=bbdir+'_'+t_str
shutil.copytree(bbdir,bak_dir)
#is_exists=os.path.exists(pub_file)
print ''
#if is_exists:
# print 'rm '+pub_file
# os.popen('rm -rf '+pub_file)
#print 'unzip:'+zip_file
#os.unzip(pubfile)
#output=os.popen('unzip '+zip_file)
#un_zip(zip_file,pub_dir)
#print 'waiting unzip ...'
#time.sleep(3)
#os.popen('mv '+file_name +' '+pub_dir)
#zip_dir='/home/gdmt/mm/py/'+file_name
#print ''
#print 'zip_dir:'+zip_dir
#print 'pub_dir:'+pub_file
#print '' #shutil.move(zip_dir,pub_dir)
#time.sleep(1)
#os.popen('mv '+zip_dir+' '+pub_dir)
#os.popen('rm -rf /home/gdmt/mm/py/'+file_name)
# start publist ...
pub_dir=pub_file
pub(pub_file)
配合自动发布的shell脚本:需要先做ssh信任
#!/bin/bash
pub_path = $1 ssh gdmt@1.1.1.19 << remotessh
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit
remotessh
echo "===================138.19 end=================================" sleep 3 ssh gdmt@1.1.1.20 << remotessh
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit
remotessh
echo "===================138.20 end=================================" sleep 3 ssh gdmt@1.1.1.21 << remotessh
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit
remotessh
echo "===================138.21 end=================================" sleep 3 ssh gdmt@1.1.1.22 << remotessh
cd /home/gdmt/mastercom/py
./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit
remotessh
echo "====================138.22 end================================" sleep 3 ssh gdmt@1.1.1.31 << remotessh
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit
remotessh
echo "===================138.31 end=================================" sleep 3 ssh gdmt@1.1.1.32 << remotessh
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit
remotessh
echo "==================138.32 end==================================" sleep 3 ssh gdmt@1.1.1.33 << remotessh
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
exit
remotessh
echo "==================138.33 end==================================" sleep 3 #ssh gdmt@1.1.1.37 << remotessh
cd /home/gdmt/mastercom/py
sudo ./pub_gdmt.py $1
sudo service tomcat stop
sudo service tomcat start
#exit
#remotessh echo "===================138.37 end=================================="
#ssh gdmt@1.1.1.33 << remotessh #exit
#remotessh
python把文件从一个目录复制到另外一个目录,并且备份的更多相关文章
- java 把一个文件夹里图片复制到另一个文件夹里
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.ut ...
- 图片的copy,从一个目录复制到另一个目录
代码: public function index(){ $path='G:/相片/2014.9.8深圳莲花山/IMG_1282.JPG'; $path=iconv('utf-8','gb2312', ...
- 将一个dropdownlist从一个div复制到另一个div
<select id="dropdwon1"> <option value=">Item1</option> <option v ...
- Python OS 文件/目录方法
Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...
- 3 Python os 文件和目录
ile 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.flush() ...
- python之文件
文件打开函数:f = open 表1-1:open函数中模式参数常用值 打开模式 描述 'r' 读模式 'w' 写模式 'a' 追加模式 'b' 二进制模式 '+' 读/写模式 表1-2:文件对象方法 ...
- Python OS 文件
Python OS 文件: 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前工作目录 3 os.chflags(path, flags) 设置路 ...
- python 读取文件read.csv报错 OSError: Initializing from file failed
小编在用python 读取文件read.csv的时候 报了一个错误 OSError: Initializing from file failed 初始化 文件失败 检查了文件路径,没问题 那应该是我文 ...
- (转)SqlServer将数据库中的表复制到另一个数据库
本文为转载地址为:http://jingyan.baidu.com/article/d5c4b52bc5c102da570dc547.html 复制表结构 在使用SqlServer的过程中,我们可能需 ...
随机推荐
- list-列表练习
#list列表取值更方便灵活 列表.数组说的都是1个东西#列表中每个字符都有一个编号,就是我们说的下标,从0开始#如果你输入的下标在列表中不存在,会报下标越界的错误 1.查询user表中下标为0的记录 ...
- 11-15 dom 动态创建节点
1.生成节点的方法 document.createElement(“div”) 2.插入节点的方法 父元素.appendChild(新节点) 在父节点中的子节点后面插入新的节点 3.在指定的位置 ...
- Idea-每次修改JS文件都需要重启Idea才能生效解决方法
最近开始使用Idea,有些地方的确比eclipse方便.但是我发现工程每次修改JS或者是JSP页面后,并没有生效,每次修改都需要重启一次Tomcat这样的确不方便.我想Idea肯定有设置的方法,不可能 ...
- RFC-RTSP
Network Working Group H. Schulzrinne Request for Comments: 2326 Columbia U. Category: Standards Trac ...
- django之模版层(template)
上篇主要介绍了django的MTV模型,主要介绍了视图层之路由配置系统url分发和视图层之视图函数view,本篇主要讲解MTV模型中的模版层template. 本篇导论: 模版简介 模版之变量 模版之 ...
- Win7或Win8上安装VS2015报“安装包丢失或损坏”问题的解决办法
原因:microsoft root certificate authority 2010.microsoft root certificate authority 2011证书未安装,导致文件校验未通 ...
- SpringCloud无废话入门04:Hystrix熔断器及监控
1.断路器(Circuit Breaker)模式 在上文中,我们人为停掉了一个provider,在实际的生产环境中,因为意外某个服务down掉,甚至某一层服务down掉也是会是有发生的.一旦发生这种情 ...
- 02、创建RDD(集合、本地文件、HDFS文件)
Spark Core提供了三种创建RDD的方式,包括:使用程序中的集合创建RDD:使用本地文件创建RDD:使用HDFS文件创建RDD. 1.并行化集合 如果要通过并行化集合来创建RDD,需要针对程序中 ...
- 【nginx&php】后台权限认证方式
一.最常用的方法(代码中限制) 1.如何限制IP function get_new_ip(){ if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('H ...
- class<T>和 class<?>类型
? 表示不确定的java类型. T 表示java类型 Class<T>在实例化的时候,T要替换成具体类Class<?>它是个通配泛型,?可以代表任何类型<? extend ...