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

文件夹常用操作

发布时间:2020-12-17 17:05:07 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #_*_encoding:utf-8_*_ #------------------------------------------------------------------------------- # Name: 文件夹常用操作 # Purpose: # #

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

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

#_*_encoding:utf-8_*_
 
#-------------------------------------------------------------------------------
 
# Name:        文件夹常用操作
 
# Purpose:
 
#
 
# Author:      QiuChangJie
 
#
 
# Created:     07/06/2015
 
# Copyright:   (c) cj.qiu 2015
 
# Licence:     <your licence>
 
#-------------------------------------------------------------------------------

import sys
import os
import shutil
import platform


class FileUtils:
    @staticmethod
    def fileFilterExt(args,dirn,fln):
        for fls in fln:
            if fls.lower().endswith(args[1].lower()) and os.path.isfile(os.path.join(dirn,fls)):
                args[0].append(os.path.join(dirn,fls))

    @staticmethod
    def dirFilterExt(args,fln):
        for fls in fln:
            if fls.lower().endswith(args[1].lower()) and os.path.isdir(os.path.join(dirn,fls))

    # 根据文件扩展名获取文件
    @staticmethod
    def getFiles(root,ext):
        fileList = list()
        os.path.walk(root,FileUtils.fileFilterExt,(fileList,ext))
        return fileList

    # 获取文件夹
    @staticmethod
    def getDirs(root,ext):
        dirList = list()
        os.path.walk(root,FileUtils.dirFilterExt,(dirList,ext))
        return dirList

    # 复制文件到指定目录
    @staticmethod
    def copyFileExt(src,dst):
        if not os.path.exists(src):
            print(str.format("%s is not exists",src))
            return

        dirList = FileUtils.getDirs(src,"")
        for d in dirList:
            subDir = d[len(src) + 1:]
            if not os.path.exists(os.path.join(dst,subDir)):
                os.mkdir(os.path.join(dst,subDir))

        fileList = FileUtils.getFiles(src,"")
        for f in fileList:
            subName = f[len(src) + 1:]
            shutil.copy(f,os.path.join(dst,subName))

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

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

(编辑:李大同)

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

    推荐文章
      热点阅读