[Python] 檔案掃描,
檔案掃描
下載
https://github.com/YouXianMing/FileManager
細節
1. 基於Python 3.60,其他版本未測試
2. 支援掃描深度,不設定則掃描全部,設定了值,則掃描具體的層級
源碼
import osimport timeclass FileObject: """ 檔案對象 """ def __init__(self, filePath): """ 建立FileObject對象 :param filePath: 檔案路徑 """ self.__tree_file_objects = None self.__scan_depth = None self.__exists = False self.__file_path = None self.__is_file = None self.__is_dir = None self._is_link = None self.__size = None self.__last_modification_time = None self.__last_access_time = None self.__metadata_change_time = None self.__file_name = None self.__dir_name = None if os.path.exists(filePath): self.__scan_depth = 0 self.__exists = True self.__file_path = filePath self.__is_file = os.path.isfile(filePath) self.__is_dir = os.path.isdir(filePath) self._is_link = os.path.islink(filePath) self.__size = os.path.getsize(filePath) self.__last_modification_time = os.path.getmtime(filePath) self.__last_access_time = os.path.getatime(filePath) self.__metadata_change_time = os.path.getctime(filePath) self.__file_name = os.path.basename(filePath) self.__dir_name = os.path.dirname(filePath) if self.__is_dir: self.__tree_file_objects = [] @property def scan_depth(self): """ 掃描深度的設定 :return: 設定的掃描深度 """ return self.__scan_depth @scan_depth.setter def scan_depth(self, depth): self.__scan_depth = depth @property def tree_file_objects(self): """ 樹形對象結構列表 :return: 數組 """ return self.__tree_file_objects @tree_file_objects.setter def tree_file_objects(self, fileList): self.__tree_file_objects = fileList @property def exists(self): """ 檔案是否存在 :return: 存在返回True,不存在返回False """ return self.__exists @property def file_path(self): """ 檔案路徑(初始化成功之後可取) :return: 初始化成功則有路徑,沒有初始化成功則沒有路徑 """ return self.__file_path @property def is_file(self): """ 是否是檔案(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,是檔案返回True,不是檔案返回False """ return self.__is_file @property def is_dir(self): """ 是否是檔案夾(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,是檔案夾返回True,不是檔案返回False """ return self.__is_dir @property def is_link(self): """ 是否是link(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,是link返回True,不是檔案返回False """ return self._is_link @property def size(self): """ 檔案大小(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,返迴文件大小 """ return self.__size @property def last_modification_time(self): """ 最後修改時間(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,返回最後修改時間 """ return self.__last_modification_time @property def last_access_time(self): """ 最後操作時間(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,返回最後操作時間 """ return self.__last_access_time @property def metadata_change_time(self): """ 最後中繼資料修改時間(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,返回最後中繼資料修改時間 """ return self.__metadata_change_time @property def file_name(self): """ 檔案名稱(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,返迴文件名 """ return self.__file_name @property def dir_name(self): """ 檔案夾名(初始化成功之後可取) :return: 未初始化成功返回None,初始化成功時,返迴文件夾名 """ return self.__dir_name def show_info(self): if self.exists: string = "[%s]\n" % self.file_name string += "------------------------------------------\n" string += "filePath : %s\n" % self.file_path string += "size : %.2f kb\n" % (self.size / 1024.0) string += "isDir : %s\n" % self.is_dir string += "isFile : %s\n" % self.is_file string += "isLink : %s\n" % self.is_link string += "lastAccessTime : %s\n" % time.strftime('%Y-%m-%d %H:%M:%S %A', time.localtime(self.last_access_time)) string += "lastModificationTime : %s\n" % time.strftime('%Y-%m-%d %H:%M:%S %A', time.localtime(self.last_modification_time)) string += "metadataChangeTime : %s\n" % time.strftime('%Y-%m-%d %H:%M:%S %A', time.localtime(self.metadata_change_time)) string += "------------------------------------------\n" print(string)class FileObjectManager: """ 用來掃描FileObject的類 """ def __init__(self, rootFile): self.rootFile = None if isinstance(rootFile, FileObject): self.rootFile = rootFile def all_file_objects(self): """ 擷取掃描出來的檔案 :return: 掃描檔案的數組 """ filesList = [] if self.rootFile: FileObjectManager.__get_all_files(self.rootFile, filesList) return filesList def scan_with_depth(self, depth=999999): """ 開始掃描 :param depth: 掃描深度 :return: FileObjectManager對象本身 """ if self.rootFile: # 掃描之前清空rootFile中的數組的資料 self.rootFile.tree_file_objects = [] self.__scan_with_depth(self.rootFile, depth) return self @staticmethod def __get_all_files(rootFile, filesList): """ 靜態方法:擷取所有檔案 :param rootFile: FileObject對象,作為rootFile傳入 :param filesList: 集合 :return: 無 """ # 判斷rootFile是否是File類型 if not isinstance(rootFile, FileObject): assert False, 'rootFile不是FileObject類型.' if type(filesList) != list: assert False, 'filesList不是List類型' # 遍曆擷取所有的檔案 for tmpFile in rootFile.tree_file_objects: filesList.append(tmpFile) if tmpFile.is_dir: FileObjectManager.__get_all_files(tmpFile, filesList) @staticmethod def __scan_with_depth(rootFile, depth): """ 靜態方法:遞迴使用的掃描方法 :param rootFile: 最為rootFile的FileObject對象 :param depth: 掃描深度 :return: 無 """ # 如果掃描等級超過了depth,則不掃描了 if rootFile.scan_depth >= depth: return # 如果rootFile是檔案夾 if rootFile.is_dir: # 擷取當前檔案夾下的所有子檔案 filePathList = os.listdir(rootFile.file_path) # 遍曆檔案並建立檔案夾 for fileName in filePathList: # 建立FileObject對象 file = FileObject(os.path.join(rootFile.file_path, fileName)) # 設定掃描深度 file.scan_depth = rootFile.scan_depth + 1 # 將此檔案添加到rootFile的treeFileObjects中 rootFile.tree_file_objects.append(file) # 如果這個檔案也是檔案夾,則遞迴調用 if file.is_dir: FileObjectManager.__scan_with_depth(file, depth)
使用
from file_manager import *# 給定檔案夾路徑filesList = FileObjectManager(FileObject("/Users/YouXianMing/Desktop")).scan_with_depth(3).all_file_objects()# 拼接路徑數組filesString = ""for file in filesList: # 如果是檔案,則列印 if file.is_file: print(file.file_path)