#!/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把文件从一个目录复制到另外一个目录,并且备份的更多相关文章

  1. java 把一个文件夹里图片复制到另一个文件夹里

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.ut ...

  2. 图片的copy,从一个目录复制到另一个目录

    代码: public function index(){ $path='G:/相片/2014.9.8深圳莲花山/IMG_1282.JPG'; $path=iconv('utf-8','gb2312', ...

  3. 将一个dropdownlist从一个div复制到另一个div

    <select id="dropdwon1"> <option value=">Item1</option> <option v ...

  4. Python OS 文件/目录方法

    Python OS 文件/目录方法 os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os. ...

  5. 3 Python os 文件和目录

    ile 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.flush() ...

  6. python之文件

    文件打开函数:f = open 表1-1:open函数中模式参数常用值 打开模式 描述 'r' 读模式 'w' 写模式 'a' 追加模式 'b' 二进制模式 '+' 读/写模式 表1-2:文件对象方法 ...

  7. Python OS 文件

    Python OS 文件: 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前工作目录 3 os.chflags(path, flags) 设置路 ...

  8. python 读取文件read.csv报错 OSError: Initializing from file failed

    小编在用python 读取文件read.csv的时候 报了一个错误 OSError: Initializing from file failed 初始化 文件失败 检查了文件路径,没问题 那应该是我文 ...

  9. (转)SqlServer将数据库中的表复制到另一个数据库

    本文为转载地址为:http://jingyan.baidu.com/article/d5c4b52bc5c102da570dc547.html 复制表结构 在使用SqlServer的过程中,我们可能需 ...

随机推荐

  1. Html块标签、含样式的标签、语义化的标签:

    Html块标签: (1)<div>标签 块元素,表示一块内容,没有具体的语义 (2)<span>标签 行内元素,表示一行中的一小段内容,没有具体的语义 <p>标签 ...

  2. 用PowerShell激活anaconda的环境

    1.以管理员身份打开PowerShell 2. 执行conda install -n root -c pscondaenvs pscondaenvs 3. 执行 Set-ExecutionPolicy ...

  3. Linux之磁盘分区篇

    作业三: 1)   开启Linux系统前添加一块大小为20G的SCSI硬盘 2)   开启系统,右击桌面,打开终端 3)   为新加的硬盘分区,一个主分区大小为10G,剩余空间给扩展分区,在扩展分区上 ...

  4. [赶集网] 【MySql】赶集网mysql开发36条军规

    [赶集网] [MySql]赶集网mysql开发36条军规 (一)核心军规(1)不在数据库做运算   cpu计算务必移至业务层:(2)控制单表数据量   int型不超过1000w,含char则不超过50 ...

  5. 部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)

    部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序   在下载的Istio安装包的samples目录中包含了示例应用程序. ...

  6. spring跨域问题

    import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Co ...

  7. Android的Databinding-数据、Map绑定

    本节主要说Collection的字符串数组.List.SparseArray.Map的绑定.先看看xml的布局. <layout xmlns:android="http://schem ...

  8. 处理器 趣事 CPU/GPU/TPU/DPU/BPU

    有消息称,阿里巴巴达摩院正在研发一款神经网络芯片——Ali-NPU,主要运用于图像视频分析.机器学习等AI推理计算.按照设计,这款芯片性能将是目前市面上主流CPU.GPU架构AI芯片的10倍,而制造成 ...

  9. VMWare 虚机迁移后Linux系统网卡启动问题

    重新安装VMWare或拷贝虚机文件后有时网卡会无法工作,主要是因为网卡的Mac地址改变了,如果系统中的网卡配置信息中有Mac的信息,则虚机的系统的网卡可能无法正常工作. 如果出现上述问题,解决办法如下 ...

  10. 【C++】C++中的数组

    目录结构: contents structure [-] 一维数组 数组的定义和初始化 数组元素的访问 数组和指针 多维数组 数组是存放相同类型的容器,数组的大小确定不变,不能随意向数组中添加元素.因 ...