files

This is a programmatic windows explorer behaviors implementation. A lots of useful recipe help you easily control file, directory, file name, select, rename, etc...

file, directory, collection of files class

class filetool.files.WinFile(abspath)[source]

Represent a file.

attributes includes:

  • self.abspath absolute path (绝对路径)
  • self.dirname parents directory name (父目录路径)
  • self.basename complete file name (文件全名)
  • self.fname the first part of file name (纯文件名)
  • self.ext file extension (文件扩展名)
  • self.atime last access time (文件最后一次被触碰的时间)
  • self.ctime create time (文件被创建的时间)
  • self.mtime last modify time (文件最后一次被修改的时间)
  • self.size_on_disk file size in bytes (文件在硬盘上的大小, 单位bytes)
  • self.md5 md5 value (文件的md5值)

Appendix, The difference of (atime, ctime, mtime):

  • access time (os.path.getatime)
  • create time (os.path.getctime)
  • modify time (os.path.getmtime)
  • When rename, cut-and-paste, all 3 time stays.
  • When edit the content, atime and mtime change, ctime stays.
  • When copy the file to a new place, atime and ctime change, mtime stays.

中文文档

Windows文件对象, 可以通过 .属性名的方式访问 绝对路径, 文件夹路径, 文件名, 扩展名, 大小。免去了使用 os.path.split 等方法的麻烦。

附录, atime, ctime, mtime的区别

  • 当文件被改名, 和剪切(剪切跟改名是一个操作), 所有3个时间都不变
  • 当文件内容被修改, atime, mtime变化, ctime不变
  • 当文件被复制到新位置时, atime, ctime变化, mtime不变
initialize()[source]

Internal method. Initialize the value of some attributes.

static use_fast_init()[source]

Set initialization mode to level1_initialize

static use_regular_init()[source]

Set initialization mode to level2_initialize

static use_slow_init()[source]

Set initialization mode to level3_initialize

static set_initialize_mode(complexity=2)[source]

Set initialization mode. Default is slow mode.

  • 1: fast mode, only file name relative
  • 2: regular mode, atime, ctime, mtime, size_on_disk
  • 3: slow mode, md5 checksum

中文文档

设置WinFile类的全局变量, 指定WinFile.initialize方法所绑定的初始化方式。

level3_initialize()[source]

Load abspath, dirname, basename, fname, ext, atime, ctime, mtime, size_on_disk attributes in initialization.

中文文档

比较全面但稍慢的WinFile对象初始化方法, 从绝对路径中取得:

  • 绝对路径
  • 父目录路径
  • 文件全名
  • 纯文件名
  • 文件扩展名
  • access time
  • create time
  • modify time
  • 文件占据磁盘大小
  • 文件的哈希值
level2_initialize()[source]

Load abspath, dirname, basename, fname, ext, atime, ctime, mtime, size_on_disk attributes in initialization.

中文文档

比较全面但稍慢的WinFile对象初始化方法, 从绝对路径中取得:

  • 绝对路径
  • 父目录路径
  • 文件全名
  • 纯文件名
  • 文件扩展名
  • access time
  • create time
  • modify time
  • 文件占据磁盘大小
level1_initialize()[source]

Load abspath, dirname, basename, fname, ext attributes in initialization.

中文文档

快速的WinFile对象初始化方法, 只从绝对路径中取得:

  • 绝对路径
  • 目录路径
  • 文件全名
  • 纯文件名
  • 文件扩展名
to_dict()[source]

Convert WinFile to dictionary.

update(new_dirname=None, new_fname=None, new_ext=None)[source]

Update property, automatically update relative property.

Parameters:
  • new_dirname – new dirname
  • new_fname – new fname
  • new_ext – new ext

中文文档

更新WinFile的属性。更新一个, 也同时更新其他相关的属性。例如更新 extension时, 也自动更新basename和abspath。

copy()[source]

Create a copy of this WinFile instance.

copy_to(dst, overwrite=False)[source]

Copy this file to another place.

Parameters:
  • dst – copy-to destination
  • overwrite – if True, silently overwrite output file
delete()[source]

Delete this winfile.

rename(new_dirname=None, new_fname=None, new_ext=None)[source]

Rename the dirname, fname, extension or their combinations.

Parameters:
  • new_dirname – new dirname
  • new_fname – new fname
  • new_ext – new ext

中文文档

对文件的父目录名, 纯文件名, 扩展名, 或它们的组合进行修改。

class filetool.files.WinDir(abspath)[source]

Represent a directory.

  • self.size_total: total size of all files
  • self.size_current_total: total size of all files, not include file in subfolder
  • self.num_folder_total: number of all directory
  • self.num_folder_current: number of all directory, not include subfolder
  • self.num_file_total: number of all file
  • self.num_file_current: number of all file, not include file in subfolder

中文文档

Windows目录对象, 可以通过 .属性名来访问 绝对路径, 目录总大小, 子目录数量, 子文件数量。免去了使用os.path.function的麻烦。

WinDir的属性:

  • self.size_total: 文件夹总大小
  • self.size_current_total: 文件夹一级子文件总大小
  • self.num_folder_total: 子文件夹数量
  • self.num_folder_current: 一级子文件夹数量
  • self.num_file_total: 子文件数量
  • self.num_file_current: 一级子文件数量
get_detail()[source]

Get general stats information.

Includes:

  • size_total: total size on disk

  • num_folder_total: how many subfolders

  • num_file_total: how many files

  • size_current: total size of files on this folder. file in subfolders

    doesn’t count

  • num_folder_current: how many files, subfolders doens’t count

  • num_file_current: how many files, file in subfolders doens’t count

rename(new_dirname=None, new_basename=None)[source]

Rename the dirname, basename or their combinations.

中文文档

对文件的目录名, 文件夹名, 或它们的组合进行修改。

class filetool.files.FileCollection(path_or_path_list=[])[source]

A container class of WinFile.

Simplify file selection, removing, filtering, sorting operations.

中文文档

WinFile的专用容器, 主要用于方便的从文件夹中选取文件, 筛选文件, 并对指定文件集排序。 当然, 可以以迭代器的方式对容器内的文件对象进行访问。

add(abspath_or_winfile, enable_verbose=True)[source]

Add absolute path or WinFile to FileCollection.

remove(abspath_or_winfile, enable_verbose=True)[source]

Remove absolute path or WinFile from FileCollection.

howmany

An alias of __len__() method.

iterfiles()[source]

Yield all WinFile object.

iterpaths()[source]

Yield all WinFile’s absolute path.

static yield_all_file_path(dir_abspath)[source]

File path iterator.

中文文档

遍历path目录下的所有文件, 返回绝对路径。

static yield_all_winfile(dir_abspath)[source]

WinFile instance iterator.

中文文档

遍历path目录下的所有文件, 返回WinFile。

static yield_all_top_file_path(dir_abspath)[source]

File path iterator, except file in subfolder.

中文文档

遍历path目录下的所有文件, 不包括子文件夹中的文件, 返回绝对路径。

static yield_all_top_winfile(dir_abspath)[source]

WinFile instance iterator, except file in subfolder.

中文文档

遍历path目录下的所有文件, 不包括子文件夹中的文件, 返回WinFile。

static yield_all_dir_path(dir_abspath)[source]

Directory path iterator.

中文文档

遍历dir_abspath目录下的所有子目录, 返回绝对路径。

static yield_all_windir(dir_abspath)[source]

WinDir instance iterator.

中文文档

遍历dir_abspath目录下的所有子目录, 返回绝对WinDir。

static yield_all_top_dir_path(dir_abspath)[source]

Directory path iterator, except directory in subfolder.

中文文档

遍历dir_abspath目录下的所有子目录, 不包括子目录中的子目录, 返回绝对路径。

static yield_all_top_windir(dir_abspath)[source]

Directory instance iterator, except directory in subfolder.

中文文档

遍历dir_abspath目录下的所有子目录, 不包括子目录中的子目录, 返回绝对WinDir。

static from_path(path_or_path_list)[source]

Create a new FileCollection and add all files from dir_path.

Parameters:path_or_path_list – absolute dir path, WinDir instance, list of absolute dir path or list of WinDir instance.

中文文档

添加dir_path目录下的所有文件到一个新的FileCollection中.

static from_path_by_criterion(path_or_path_list, criterion, keepboth=False)[source]

Create a new FileCollection, and select some files from dir_path.

How to construct your own criterion function:

def filter_image(winfile):
    if winfile.ext in [".jpg", ".png", ".bmp"]:
        return True
    else:
        return False

fc = FileCollection.from_path_by_criterion(dir_path, filter_image)
Parameters:
  • path_or_path_list – absolute dir path, WinDir instance, list of absolute dir path or list of WinDir instance.
  • criterion (function) – customize filter function
  • keepboth (boolean) – if True, returns two file collections, one is files with criterion=True, another is False.

中文文档

直接选取dir_path目录下所有文件, 根据criterion中的规则, 生成 FileCollection。

static from_path_except(path_or_path_list, ignore=None, ignore_ext=None, ignore_pattern=None)[source]

Create a new FileCollection, and select all files except file matching ignore-rule:

dir_path = "your/path"
fc = FileCollection.from_path_except(
    dir_path, ignore=["test"], ignore_ext=[".log", ".tmp"]
    ignore_pattern=["some_pattern"])
Parameters:
  • path_or_path_list – absolute dir path, WinDir instance, list of absolute dir path or list of WinDir instance.
  • ignore – file or directory defined in this list will be ignored.
  • ignore_ext – file with extensions defined in this list will be ignored.
  • ignore_pattern – any file or directory that contains this pattern will be ignored.

中文文档

选择dir_path下的所有文件, 在ignore, ignore_ext, ignore_pattern中所定义 的文件将被排除在外。

static from_path_by_pattern(path_or_path_list, pattern=None)[source]

Create a new FileCollection, and select all files except file matching ignore-rule:

dir_path = "your/path"
fc = FileCollection.from_path_by_pattern(
    dir_path, pattern=["log"])
Parameters:
  • path_or_path_list – absolute dir path, WinDir instance, list of absolute dir path or list of WinDir instance.
  • pattern – any file or directory that contains this pattern will be selected.

中文文档

选择dir_path下的所有文件的相对路径中包含有pattern的文件。

static from_path_by_size(path_or_path_list, min_size=0, max_size=1099511627776)[source]

Create a new FileCollection, and select all files that size in a range:

dir_path = "your/path"

# select by file size larger than 100MB
fc = FileCollection.from_path_by_size(
    dir_path, min_size=100*1024*1024)

# select by file size smaller than 100MB
fc = FileCollection.from_path_by_size(
    dir_path, max_size=100*1024*1024)

# select by file size from 1MB to 100MB
fc = FileCollection.from_path_by_size(
    dir_path, min_size=1024*1024, max_size=100*1024*1024)
Parameters:
  • path_or_path_list – absolute dir path, WinDir instance, list of absolute dir path or list of WinDir instance.
  • min_size – any file size greater than this value will return
  • max_size – any file size less than this value will return
static from_path_by_ext(path_or_path_list, ext)[source]

Create a new FileCollection, and select all files that extension matching ext:

dir_path = "your/path"

fc = FileCollection.from_path_by_ext(dir_path, ext=[".jpg", ".png"])
Parameters:
  • path_or_path_list – absolute dir path, WinDir instance, list of absolute dir path or list of WinDir instance.
  • ext – select file by extension
static from_path_by_md5(path_or_path_list, md5_value)[source]

Create a new FileCollection, and select all files’ that md5 is matching.

中文文档

给定一个文件使用WinFile模块获得的md5值, 在list_of_dir中的文件里, 找到与之相同的文件。

sort_by(attr_name, reverse=False)[source]

Sort files by one of it’s attributes.

中文文档

对容器内的WinFile根据其某一个属性升序或者降序排序。

sort_by_abspath(reverse=False)[source]

中文文档

对WinFile根据 绝对路径 进行排序。

sort_by_dirname(reverse=False)[source]

中文文档

对WinFile根据 父目录路径 进行排序。

sort_by_fname(reverse=False)[source]

中文文档

对WinFile根据 纯文件名 进行排序。

sort_by_ext(reverse=False)[source]

中文文档

对WinFile根据 文件扩展名 进行排序。

sort_by_atime(reverse=False)[source]

中文文档

对WinFile根据 文件最后一次被触碰的时间 进行排序。

sort_by_ctime(reverse=False)[source]

中文文档

对WinFile根据 文件被创建的时间 进行排序。

sort_by_mtime(reverse=False)[source]

中文文档

对WinFile根据 文件最后一次被修改的时间 进行排序。

sort_by_size(reverse=False)[source]

中文文档

对WinFile根据 文件在硬盘上的大小 进行排序。

select(criterion, keepboth=False)[source]

Filter current file collections, create another file collections contains all winfile with criterion=True.

How to construct your own criterion function, see FileCollection.from_path_by_criterion().

Parameters:
  • criterion (function) – customize filter function
  • keepboth (boolean) – if True, returns two file collections, one is files with criterion=True, another is False.

中文文档

在当前的文件集合中, 根据criterion中的规则, 选择需要的生成 FileCollection。当keepboth参数=True时, 返回两个FileCollection, 一个 是符合条件的文件集合, 一个是不符合条件的。

static show_big_file(dir_path, threshold)[source]

Print all file path that file size greater and equal than #threshold.

static show_patterned_file(dir_path, pattern=[], filename_only=True)[source]

Print all file that file name contains pattern.

static create_fake_mirror(src, dst)[source]

Copy all dir, files from src to dst. But only create a empty file with same file name. Of course, the tree structure doesn’t change.

A recipe gadget to create some test data set.

Make sure to use absolute path.

中文文档

复制整个src目录下的文件树结构到dst目录。但实际上并不复制内容, 只复制 文件名。即, 全是空文件, 但目录结构一致。

class filetool.files.FileFilter[source]

File filter container class.

static image(winfile)[source]

Image file filter.

static audio(winfile)[source]

Audio file filter.

static video(winfile)[source]

Video file filter.

static pdf(winfile)[source]

Pdf file filter.

static word(winfile)[source]

Microsoft Word file filter.

static excel(winfile)[source]

Microsoft Excel file filter.

static ppt(winfile)[source]

Microsoft Power Point file filter.

static archive(winfile)[source]

Compressed archive file filter.