加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

用Python 实现的目录拷贝程序

发布时间:2020-12-17 17:15:53 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 用python写的小程序,可以用这个程序在固定的目录之间来回的拷贝. copy one directory to another directory @author: ''' import os; import shutil,e

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

用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:sourcecodeFTS";  
office_disk = "E:FTS_HOME";  
  
other_source = "D:pythontestsource";  
other_target = "D:pythontesttarget";  
  
FILES_DIRECTORY_LIST = ["ForeignTradeFrontsrc","ForeignTradeFrontWebContent","ForeignTradeServerejbModule","ForeignTradeServerTestSrc"];  
  
  
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();  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读