pyhon搜索目录下的文件或文件夹

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huapeng_guo/article/details/84555761

pyhon搜索目录下的文件或文件夹

# !/usr/bin/env python
# -*-coding:utf-8-*-
"""
Copyright(c)2018,浙江大华
file:     sambaCenter.py
author:   27711
date:     2018/11/24
version:  1.0
describe:

"""

import os
import subprocess

class CFileInfo(object):
    def __int__(self):
        self.fileType = 0  # 0:dir 1:file
        self.fileName = ""
        self.fileSize = 0  # Byte# ,if dir 0
        self.parentDir = ""  # "/home/10001"


class CSambaCenter(object):
    def __init__(self):
         pass

    def scanFiles(self, userId, dir="/home", preFix=None, postFix=None):
        """
        搜索指定目录下的匹配前缀或后缀的文件和文件夹
        :param userId: 用户ID 如10001
        :param dir:指定目录,若为文件则返回文件详情
        :param preFix:前缀如:“test_”
        :param postFix:后续如:".py"
        :return:目录和文件的list详情
        """
        try:
            if preFix is None and postFix is None:
                return self.getAllDirs(userId, dir)

            listFilesInfo = []
            for dirpath, dirs, files in os.walk(dir):
                for dirname in dirs:
                    if dirname[0] == '.':  # 剔除隐藏
                        pass
                    if postFix:
                        if dirname.endswith(postFix):
                            fileInfo = CFileInfo()
                            fileInfo.fileName = dirname
                            fileInfo.parentDir = dirpath
                            listFilesInfo.append(fileInfo)
                    elif preFix:
                        if dirname.startswith(preFix):
                            fileInfo = CFileInfo()
                            fileInfo.fileName = dirname
                            fileInfo.parentDir = dirpath
                            listFilesInfo.append(fileInfo)

                for file in files:
                    if file[0] == '.':  # 剔除隐藏
                        pass
                    if postFix:
                        if file.endswith(postFix):
                            fileInfo = CFileInfo()
                            fileInfo.fileType = 1
                            fileInfo.fileSize = os.path.getsize(file)
                            fileInfo.fileName = file
                            fileInfo.parentDir = dirpath
                            listFilesInfo.append(fileInfo)
                    elif preFix:
                        if file.startswith(preFix):
                            fileInfo = CFileInfo()
                            fileInfo.fileType = 1
                            fileInfo.fileSize = os.path.getsize(file)
                            fileInfo.fileName = file
                            fileInfo.parentDir = dirpath
                            listFilesInfo.append(fileInfo)

            return listFilesInfo
        except Exception as e:
            print(e)
            return None


if __name__ == '__main__':
    pass

剔除隐藏的文件和文件夹,for dirpath, dirs, files in os.walk(dir),通过python库接口方便,方案性能上需要再优化,若文件较多,不建议次方案

猜你喜欢

转载自blog.csdn.net/huapeng_guo/article/details/84555761