用Python 实现的目录拷贝程序

背景:最近自己在做一个项目,由于时间紧,任务重,所以会利用在公司空余的时间去做,由于公司不能访问一些免费的代码管理的网站,所以天天只能用u盘来回的拷贝在公司和家里做的修改,随着文件的慢慢增加,拷贝的时间也越来越长,而且拷贝的时候会浪费很多的时间拷贝class文件,正好最近在学习python,所以用python写了个小程序,可以用这个程序在固定的目录之间来回的拷贝.

程序如下:

'''
copy one directory to another directory

@author: 
'''
import os;
import shutil, errno;
import ctypes;
import itertools;
import string;
import platform;

_home = "E:\sourcecode";
home_disk = "K:\FTS_HOME";
office = "D:\sourcecode\FTS";
office_disk = "E:\FTS_HOME";

other_source = "D:\\pythontest\\source";
other_target = "D:\\pythontest\\target";

FILES_DIRECTORY_LIST = ["ForeignTradeFront\src", "ForeignTradeFront\WebContent", "ForeignTradeServer\ejbModule", "ForeignTradeServer\TestSrc"];


def copyFiles():
    directory = input("how to copy files? \n\
copy to office(1), \n\
copy to office removable driver(2),\n\
copy to _home(3),\n\
copy to _home removable driver(4);\n\
other(5)");
    
    if directory == "1":
        executeCopyFiles(office_disk, office);
    elif directory == "2":
        executeCopyFiles(office, office_disk);
    elif directory == "3":
        executeCopyFiles(home_disk, _home);
    elif directory == "4":
        executeCopyFiles(_home, home_disk);
    elif directory == "5":
        executeCopyFiles(other_source, other_target);
        
  
def executeCopyFiles(sourceDirectory, targetDirectory):
    copyConfirm = input("Are you sure want copy" + sourceDirectory + " to " + targetDirectory + "?(Y/N)");
    if copyConfirm == "Y":
        if os.path.isdir(sourceDirectory) and  os.path.isdir(targetDirectory):
            for update_directory in FILES_DIRECTORY_LIST:
                newSourceDirectory = generateUpdatePath(sourceDirectory, update_directory) ;
                newTargetDirectory = generateUpdatePath(targetDirectory, update_directory) ;
                deleteOldTargetDirectory(newTargetDirectory);
                copyanything(newSourceDirectory, newTargetDirectory);
        else:
            print("Please input a directionary path!");
    else:
        print("Cancellation copy file!");
        
def generateUpdatePath(originalPath, updatePath):
    return os.path.join(originalPath, updatePath);

def deleteOldTargetDirectory(targetDirectory):
    print(targetDirectory + " was removed!");
    shutil.rmtree(targetDirectory, True);
      
def copyanything(src, dst):
    try:
        if(os.path.exists(dst)):
            shutil.rmtree(dst, False);
        shutil.copytree(src, dst)
        print(dst + " copy successful");
    except OSError as exc: # python >2.5
        if exc.errno == errno.ENOTDIR:
            shutil.copy(src, dst)
        else: raise

if __name__ == '__main__':
    copyFiles();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值