zoukankan      html  css  js  c++  java
  • Python脚本:linux上将筛选的文件夹复制到另一个目录,保存目录结构以及文件和文件夹操作方法

    import os,shutil
    
    def newDir(dir_path):
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
    
    def copydir(where_path,go_path,start_time,end_time):
        newDir(go_path)
        for brand in os.listdir(where_path):
            brand_path = os.path.join(where_path, brand)
            #print('brand_path',brand_path)
            for site in os.listdir(brand_path):
                site_path = os.path.join(brand_path,site)
                #print('site_path',site_path)
                for child in os.listdir(site_path):
                    file_time = '-'.join(child.split('-')[2:5])
                    go_dir = go_path + '/' + brand + '/' + site#动态生成子目录
                    print(file_time[:10])
                    if file_time[:10] >= start_time and file_time[:10] <= end_time:
                        child_path = os.path.join(site_path,child)
    
                        if not os.path.isdir(child_path):
                            #print('file',child_path)
                            newDir(go_dir)
                            shutil.copy(child_path,go_dir)
                        else:
                            #print('dir',child_path)
                            #复制文件夹能够自己生成目录
                            shutil.copytree(child_path,go_dir+'/'+child)
    
    #删除文件名包含2018的文件和文件夹
    def deldir(path):
        for child in os.listdir(path):
            child_path = os.path.join(path,child)
            if '2018' in child:
                if not os.path.isdir(child_path):
                    os.unlink(child_path)
                else:
                    shutil.rmtree(child_path)
    
    if __name__ == "__main__":
        copydir('/lingtian/static_files/mkcms_dev/memo/memo_htmls','/lingtian/static_files/mkcms_dev','2018-09-01','2018-09-30')
        #deldir('/lingtian/static_files/mkcms_dev')                            
    

     我要复制的目录三层,所以有三层循环,保留了原来的目录结构

    附上python相关文件操作,文件的复制和移动使用shutil包 ,删除使用os包

    #文件、文件夹的移动、复制、删除、重命名 
    #导入shutil模块和os模块
    import shutil,os 
    #复制单个文件 
    shutil.copy("C:\a\1.txt","C:\b") 
    #复制并重命名新文件 
    shutil.copy("C:\a\2.txt","C:\b\121.txt") 
    #复制整个目录(备份) 
    shutil.copytree("C:\a","C:\b\new_a") 
    #删除文件 
    os.unlink("C:\b\1.txt") 
    os.unlink("C:\b\121.txt") 
    #删除空文件夹 
    try: 
        os.rmdir("C:\b\new_a") 
    except Exception as ex: 
        print("错误信息:"+str(ex))
    #提示:错误信息,目录不是空的 
    #删除文件夹及内容 
    shutil.rmtree("C:\b\new_a") 
    #移动文件
    shutil.move("C:\a\1.txt","C:\b") 
    #移动文件夹 
    shutil.move("C:\a\c","C:\b") 
    #重命名文件 shutil.move("C:\a\2.txt","C:\a\new2.txt") 
    #重命名文件夹 
    shutil.move("C:\a\d","C:\a\new_d") 
  • 相关阅读:
    【转载】Modelsim之 DO文件简介
    【原创】Quartus与Modelsim的联合仿真及脚本
    【原创】Quartus II 简单设计流程
    【原创】Modelsim仿真简单流程
    【原创】静态时序分析整理一
    【原创】DC的一些命令
    【转载】建立时间与保持时间
    linux 创建链接命令 ln -s 软链接
    Vim常用命令
    gvim查找与替换命令
  • 原文地址:https://www.cnblogs.com/lelexiu/p/10216837.html
Copyright © 2011-2022 走看看