glob&shutil学习

主要介绍python标准库中负责文件查找的高级库glob和负责文件复制删除移动的shutil,最后也简单介绍了linux的软链接

glob

glob是python里面文件管理的模块,属于内置包

学习这个模块是希望代替os.path

https://docs.python.org/3/library/glob.html

The [glob](https://docs.python.org/3/library/glob.html#module-glob) module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell

返回的文件或者文件夹都是相对路径

函数介绍

主要有三个函数

__all__ = ["glob", "iglob", "escape"]
  1. glob查找符合条件的所有文件,返回list ()

    • * 通配符
    • [ ] 类似正则的匹配 如:[0-9]匹配数字
    • 匹配单字符
    • ** recursive=True 多级匹配
    def glob(pathname, *, recursive=False):
        """Return a list of paths matching a pathname pattern.
    
        The pattern may contain simple shell-style wildcards a la
        fnmatch. However, unlike fnmatch, filenames starting with a
        dot are special cases that are not matched by '*' and '?'
        patterns.
    
        If recursive is true, the pattern '**' will match any files and
        zero or more directories and subdirectories.
        """
        return list(iglob(pathname, recursive=recursive))
    
  2. iglob 获取一个迭代器(glob调用了它并转化为list,而iglob实际是一个生成器,文件非常多的时候用这个)

    def iglob(pathname, *, recursive=False):
        """Return an iterator which yields the paths matching a pathname pattern.
    
        The pattern may contain simple shell-style wildcards a la
        fnmatch. However, unlike fnmatch, filenames starting with a
        dot are special cases that are not matched by '*' and '?'
        patterns.
    
        If recursive is true, the pattern '**' will match any files and
        zero or more directories and subdirectories.
        """
        it = _iglob(pathname, recursive, False)
        if recursive and _isrecursive(pathname):
            s = next(it)  # skip empty string
            assert not s
        return it
    

Problem

1. 如何实现多级的glob?

files = glob.glob("./**", recursive=True)

2. 是否可以获取文件夹

files = glob.glob("./*")  # 文件+文件夹
filter(os.path.isdir, files)

shutil

shutil是一个高级的文件操作库,也是属于python标准库的

__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
           "copytree", "move", "rmtree", "Error", "SpecialFileError",
           "ExecError", "make_archive", "get_archive_formats",
           "register_archive_format", "unregister_archive_format",
           "get_unpack_formats", "register_unpack_format",
           "unregister_unpack_format", "unpack_archive",
           "ignore_patterns", "chown", "which", "get_terminal_size",
           "SameFileError"]

glob如果说是文件的查找,那么shutil是文件的增删改(复制、删除、移动)

函数介绍

  • shutil.copyfile
    copyfile(src, dst, *, follow_symlinks=True)
    文件内容复制(不包括元数据) src→dst,如果dst是链接且follow_symlinks为True,则创建符号链接而非拷贝(这个比较有用,比如我的数据在/data目录下,而我fork的代码调用的是文件夹内部的data,可以使用软链接的方式,软链接将在最后介绍)

  • copymode copystat
    文件属性的拷贝

  • copy = copymode + copyfile (文件创建修改时间不会copy)

  • copy2 保留全部元数据的复制

  • copytree 文件夹的树结构的拷贝
    copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

    • symlinks True的时候拷贝软链接,False的时候拷贝链接内容(不支持软连接的平台此设置无用)
    • ignore接受glob风格的文件目录,可配合ignore_patterns使用
  • ignore_patterns工厂函数,接受glob类似匹配的patterns字符串

    copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
    
  • rmtree 删除文件夹
    rmtree(``*path*``, ``*ignore_errors=False*``, ``*onerror=None*``)

  • move 移动文件夹或目录

    • shutil.move(``*src*``, ``*dst*``, ``*copy_function=copy2*``)

软链接

linux中有软链接和硬链接,硬链接类似于指针,linux中,文件内容和文件名类似于指针与内容的关系,硬链接创建了新的一个指向原内容的指针,删除原文件名后内容并不会删除,而且不允许给目录创建硬链接

类似于windows快捷键的机制,访问软链接就是访问源文件本身,删除软链接不会删除源文件,可以实现一个文件,多处引用

# 创建软链接
ln -s 【目标目录】 【软链接地址】 
# 删除
rm -rf 【软链接地址】
# 找到当前文件夹下所有软链接(参考:https://blog.csdn.net/menghefang/article/details/88624161)
ls -alR | grep ^l